Code

Opened 2 years ago

Closed 2 years ago

Last modified 2 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]

Attachments (0)

Change History (5)

comment:1 follow-up: Changed 2 years ago by lukeplant

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Resolution set to invalid
  • Status changed from new to closed

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 2 years ago by anatoly techtonik <techtonik@…>

  • Resolution invalid deleted
  • Status changed from closed to reopened
  • Summary changed from syndication framework: no way to reverse feed url to Add a way to reverse URLs for callables
  • Type changed from Bug to New 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 2 years ago by 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 2 years ago by carljm

  • Resolution set to wontfix
  • Status changed from reopened to closed

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 2 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

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.