1

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?

0

1 Answer 1

7

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!

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.