Opened 12 years ago
Closed 12 years ago
#20645 closed Uncategorized (invalid)
reverse URL broken when you use includes
| Reported by: | Owned by: | nobody | |
|---|---|---|---|
| Component: | Template system | Version: | 1.5 |
| 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 (last modified by )
if you use an include in your base URLConf and then use a name='' in that subsequent URLConf, the name doesn't resolve when you do a reverse match in the url tag in a template.
Example:
Root
urls.py
urlpatterns = patterns('',
url(r'^bar/', include('bar.urls')),
)
Bar
urls.py
urlpatterns = patterns('bar.views',
url(r'^makeFoo/$', foo, name="generateFoo"),
)
in a HTML Template
{% url "generateFoo" %}
This generates a NoReverseMatch
If you put that named url in the root url patterns, the Reverse will find it and render it normally.
Change History (3)
comment:1 by , 12 years ago
| Description: | modified (diff) |
|---|
comment:2 by , 12 years ago
I've kept digging into this and it seems to be the '$' after the trailing slash if you do an include(). If that is there, the reverse won't be found. So in the above example, I forgot to add bar/$ which causes it to break. If the "$" is not there, the reverse will be found like normal.
Is this how it is supposed to work? It seems that this regular expression shouldn't be taken into consideration when using a reverse url matching via the 'name' or view name. Any comment by a senior Django dev about this would be useful.
comment:3 by , 12 years ago
| Resolution: | → invalid |
|---|---|
| Status: | new → closed |
Please read the documentation first (https://docs.djangoproject.com/en/1.5/topics/http/urls/) and ask support questions via the right channels (https://code.djangoproject.com/wiki/TicketClosingReasons/UseSupportChannels).
The reported case works (as in reverses the URL and doesn't raise NoReverseMatch) with both "generateFoo" entry having and not having the trailing $ although the latter makes no sense (the details are in the documentation):
In [1]: from django.core.urlresolvers import reverse
In [2]: reverse('generateFoo')
Out[2]: '/bar/makeFoo/'
In [3]: from django.template import Context, Template
In [4]: t = Template("{% url 'generateFoo' %}")
In [5]: t.render(Context())
Out[5]: u'/bar/makeFoo/'
Made description readable.