[django1.9] Url template tag, dotted path and decorator

48 views
Skip to first unread message

François Magimel

unread,
Apr 23, 2016, 1:47:26 PM4/23/16
to django...@googlegroups.com
Hi!

I'm facing an issue reversing a dotted path with django 1.9.4. Here are more details.

I pass a dotted path to the django template tag "url":
"{% url 'my.app.view.func' arg %}"
And the function "my.app.view.func" has a decorator which is using "arg". My problem is : when displaying my template, I get a NoReverseMatch error.
But I don't have any error:
- when I use the name of the url (defined in my urls.py file) or
- when I use the dotted path and remove my (simple) decorator.

I know that passing a dotted path to url is deprecated since django 1.8 [0][1], but not removed yet. So, I think there is something wrong with dotted path and decorators, but I can't find what…

PS: my decorator only redirects to another view…

Thanks,
François


[0] https://docs.djangoproject.com/en/1.8/releases/1.8/#passing-a-dotted-path-to-reverse-and-url
[1] https://docs.djangoproject.com/en/1.8/ref/templates/builtins/#url

Camilo Torres

unread,
Apr 23, 2016, 9:54:20 PM4/23/16
to Django users, magimel....@gmail.com
Hi,
May be you can use functools.wraps from Python standard library.

knbk

unread,
Apr 24, 2016, 10:06:46 AM4/24/16
to Django users, magimel....@gmail.com
In order to reverse a view by its dotted path, Django needs to know the actual path of the function. It uses the magic view.__module__ and view.__name__ attributes to determine the name. If your decorator doesn't copy these attributes from the inner function to the decorated function, Django doesn't know the path to the original function, and you can't reverse by dotted path.

As mentioned by Camilo, you can use functools.wraps() to fix this issue. 
Reply all
Reply to author
Forward
0 new messages