Opened 5 years ago

Closed 5 years ago

Last modified 5 years ago

#17361 closed New feature (wontfix)

Add a way to reverse URLs for callables

Reported by: anatoly techtonik <techtonik@…> Owned by: nobody
Component: contrib.syndication Version: 1.2
Severity: Normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

It is impossible to get reverse URL for a mapping defined like the one below:

    (r'^rss/all$', codereview.feeds.AllFeed()),

{%url codereview.feeds.AllFeed %} in template gives the following error:

NoReverseMatch while rendering: Reverse for 'codereview.feeds.AllFeed' with arguments '()' and
keyword arguments '{}' not found. [TemplateSyntaxError]

Change History (5)

comment:1 Changed 5 years ago by Luke Plant

Resolution: invalid
Status: newclosed

Yes, I believe it is impossible to reverse URLs defined only by a callable in the template. The solution is to either:

1) refer to your view by a string (dotted path) - which is possible in this case if you do something like codereview_feeds = codereview.feeds.AllFeed() in the relevant views.py. Or:

2) Give your URL a name.

comment:2 Changed 5 years ago by anatoly techtonik <techtonik@…>

Resolution: invalid
Status: closedreopened
Summary: syndication framework: no way to reverse feed urlAdd a way to reverse URLs for callables
Type: BugNew feature

(converting to feature request)
Is it possible to extract module name of the callable to build its full path to be used in reverse lookups?

comment:3 Changed 5 years ago by Michael Manfre

This feature request has two very simple workarounds as pointed out by lukeplant. It also doesn't have a way of supporting a lambda, which is not an uncommon use case. This also seems to be a lot more fragile than simply giving the url a name.

comment:4 Changed 5 years ago by Carl Meyer

Resolution: wontfix
Status: reopenedclosed

I don't think there's any reason to adopt the maintenance burden for the code that would be needed to do this. Named urls are the preferred method for reversing.

comment:5 in reply to:  1 Changed 5 years ago by anatoly techtonik <techtonik@…>

Replying to lukeplant:

Yes, I believe it is impossible to reverse URLs defined only by a callable in the template. The solution is to either:

1) refer to your view by a string (dotted path) - which is possible in this case if you do something like codereview_feeds = codereview.feeds.AllFeed() in the relevant views.py. Or:

I need to refer to codereview.feeds.AllFeed() from base template, which is inherited by many views. Cooking this variable in every view is ugly.

2) Give your URL a name.

That's also ugly, because it duplicates the code for every view. I looked at the code that does URL reversing and IMO it is overarchitectured, so I am not surprised you're not too enthusiastic to support that.

BTW, I am not the first to ask about reverse URLs for syndication framework:

  1. https://groups.google.com/forum/#!topic/django-users/Z9eJczJvtI8
  2. http://stackoverflow.com/questions/3402362/how-to-reverse-django-feed-url
  3. http://stackoverflow.com/questions/2604823/django-reverse-lookup-url-of-feeds
Note: See TracTickets for help on using tickets.
Back to Top