28

I am wondering if it is possible to have the standard url patterns spread across multiple files (in this case the project-wide urls.py and several apps-specific urls.py).

Imagine that the project urls.py look like this (got this working):

from django.conf.urls import patterns, include, url
admin.autodiscover()

urlpatterns = patterns('',
    url(r'^user/signup/', 'registration.views.signup'),
    url(r'^user/confirm/(?P<code>\w{20})/', 'registration.views.confirm'),
    url(r'^user/profile/(\d+)/', 'profile.views.show'),
    url(r'^user/profile/edit/', 'profile.views.edit'), 
)

As you can see, I have two different apps that both want to user the urls for /user/*, so I can't just use r'^user/' with an include.

My question is: Can I split the above into two seperate urls.py files, that each go into their respective app?

Note: Disregard any syntax mistakes as this was typed in

2 Answers 2

52

Sure. URLs are processed in order, and two includes can have the same prefix - if one doesn't succeed in matching, processing will just move on to the next one.

urlpatterns = patterns('',
    url(r'^user/', include('registration.urls')),
    url(r'^user/', include('profile.urls')),
)
Sign up to request clarification or add additional context in comments.

5 Comments

Ah, so I could technically just add url(r'^$', include('something')) if I wanted to add urls with various prefixes (imagine user/, profile/ and reset/ in the same app).
Yes, except without the $ - you don't want it for includes, as it terminates the match.
Thanks for giving me the hint that this should work. Except that it doesn't when you've set a namespace. My workaround for this is to collect all patterns with the same prefix in one urlpatterns variable and then include it using a namespace.
@ephes It works for me if you pass namespace as second argument to include like iDevFS points out, in his answer
Can we set same namespace for both url ? Like this: urlpatterns = patterns('', url(r'^user/', include('registration.urls'), namespace="user"), url(r'^user/', include('profile.urls'), namespace="user"), )
8

Also i suggest to add a namespace like this:

urlpatterns = patterns('',
    url(r'^user/', include('registration.urls', namespace="registration")),
    url(r'^user/', include('profile.urls', namespace="profile")),
)

2 Comments

what is the namespace for?
@eugene It makes referencing URLs easier. In this example, if the registration app contains the URL defined as re_path(r'^register/$', views.register, name='register'), it can be referenced in your Python code using reverse('registration:register') or in your templates using {% url 'registration:register' %}. Basically, you're giving your URLs and groups of URLs names, rather than typing the whole URL every time and having to go back and edit every single use of the URL when its definition changes.

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.