Reputation: 33275
I've inherited a Django application that has entries like this in urls.py
:
url(r'/abc$', 'app.views.foo', name='foo'),
url(r'/def$', 'app.views.foo', name='foo'),
url(r'^/something$|/other$', 'app.views.foo', name='foo'),
So not only do I have several url patterns that are named 'foo', but some patterns also contain a regexp that can match several different urls.
If I use reverse('foo'), which one will I get?
Upvotes: 1
Views: 2565
Reputation: 736
Doing a quickly test, it returned the "last one". You can test it pretty easy. In your example you have syntax errors "." instead of "," after the first 2 patterns. BTW, You SHOULD NOT have urls with the same name in the same APP, the idea of the name is to get an URL from a "unique name" which should represent the URL. You could have URLs with the same name in different apps and use the "namespace" parameter to reverse then.
For Example:
....
url(r'^app1/', include('apps.app1.urls', namespace='app1', app_name='app1')),
url(r'^app2/', include('apps.app2.urls', namespace='app2', app_name='app2')),
then suppose that each app's url.py file contain a name="edit" entry, you could do:
reverse("app1:edit") --> app1/edit/
reverse("app2:edit") --> app2/edit/
have fun!
Upvotes: 7