Sunday, April 24, 2016

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

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. 

On Sunday, April 24, 2016 at 3:54:20 AM UTC+2, Camilo Torres wrote:
On Saturday, April 23, 2016 at 1:17:26 PM UTC-4:30, François Magimel wrote:
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

Hi,
May be you can use functools.wraps from Python standard library.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/17117f55-47ee-4fe8-9550-b9d9f9650c9b%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment