url template tag, unhelpful when failing

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

url template tag, unhelpful when failing

Mikael Moutakis
Hi
I've running from trunk and recently switched to the NFA-branch. I found that the URL templatetag is unhelpful when failing to match a url. I patched my own copy of the function URLNode in django.template.defaulttags to return a more helpful error message instead of a empty string. Should I file a ticket with the patch or should I add the solution as a django snippet instead? Sorry, if this is a stupid question, but I'm new to this. I modified the last 3 lines of the function below.


class URLNode(Node):
    def __init__(self, view_name, args, kwargs):
        self.view_name = view_name
        self.args = args
        self.kwargs = kwargs

    def render(self, context):
        from django.core.urlresolvers import reverse, NoReverseMatch
        args = [arg.resolve(context) for arg in self.args]
        kwargs = dict([(smart_str(k,'ascii'), v.resolve(context))
                       for k, v in self.kwargs.items()])
        try:
            return reverse(self.view_name, args=args, kwargs=kwargs)
        except NoReverseMatch:
            try:
                project_name = settings.SETTINGS_MODULE.split('.')[0]
                return reverse(project_name + '.' + self.view_name,
                               args=args, kwargs=kwargs)
            except NoReverseMatch:
                if settings.DEBUG:
                    return u"/NOURLMATCH %s %s %s" % (self.view_name, args.__str__(), kwargs.__str__())
                return "" 

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Django developers" group.
To post to this group, send email to [hidden email]
To unsubscribe from this group, send email to [hidden email]
For more options, visit this group at http://groups.google.com/group/django-developers?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply | Threaded
Open this post in threaded view
|

Re: url template tag, unhelpful when failing

Malcolm Tredinnick


On Fri, 2008-07-18 at 19:30 +0200, Mikael Moutakis wrote:
> Hi
> I've running from trunk and recently switched to the NFA-branch. I
> found that the URL templatetag is unhelpful when failing to match a
> url. I patched my own copy of the function URLNode in
> django.template.defaulttags to return a more helpful error message
> instead of a empty string. Should I file a ticket with the patch or
> should I add the solution as a django snippet instead? Sorry, if this
> is a stupid question, but I'm new to this. I modified the last 3 lines
> of the function below.


The basic design rule is that template tags should always fail silently
if there's an error. Some templates and applications rely on this
behaviour. So a change like this runs against that principle.

Regards,
Malcolm




--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Django developers" group.
To post to this group, send email to [hidden email]
To unsubscribe from this group, send email to [hidden email]
For more options, visit this group at http://groups.google.com/group/django-developers?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply | Threaded
Open this post in threaded view
|

Re: url template tag, unhelpful when failing

Waylan Limberg

On Fri, Jul 18, 2008 at 3:09 PM, Malcolm Tredinnick
<[hidden email]> wrote:

>
>
> On Fri, 2008-07-18 at 19:30 +0200, Mikael Moutakis wrote:
>> Hi
>> I've running from trunk and recently switched to the NFA-branch. I
>> found that the URL templatetag is unhelpful when failing to match a
>> url. I patched my own copy of the function URLNode in
>> django.template.defaulttags to return a more helpful error message
>> instead of a empty string. Should I file a ticket with the patch or
>> should I add the solution as a django snippet instead? Sorry, if this
>> is a stupid question, but I'm new to this. I modified the last 3 lines
>> of the function below.
>
>
> The basic design rule is that template tags should always fail silently
> if there's an error. Some templates and applications rely on this
> behaviour. So a change like this runs against that principle.
>
In other words, a patch like that will likely not get applied.
However, others may find it useful, so a snippet may be the way to go.
However, I wouldn't just post your patch as a snippet, but post a full
working tag that would be added to a users app, which can then be used
in place of the one that ships with Django.

If your interested in more specifics as to why Django has chosen
silent failure, search the list archives. Seems there was a recent
discussion about it.
--
----
Waylan Limberg
[hidden email]

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Django developers" group.
To post to this group, send email to [hidden email]
To unsubscribe from this group, send email to [hidden email]
For more options, visit this group at http://groups.google.com/group/django-developers?hl=en
-~----------~----~----~----~------~----~------~--~---