简体   繁体   中英

Django URL pattern with different roots

I have two URL patterns that both exist in the same application that I'm working on getting set up.

I need urls like the following to work.

However, both of these live in the same django application.

My main urls.py looks something like this for handling the /p/12345 urls.

urlpatterns = patterns('',

(r'^p/', include('myproject.myapp.urls')),
)

and my urls.py for the application is similar. but this still only handles the /p/12345 urls.

urlpatterns = patterns('myproject.myapp.views',

(r'^(?P<object_id>\d+)/$', 'some_view'),
)

My issue is that both are almost identical but just have a different prefixes. How can I do this for both the /p/12345 and /s/12345 urls. I've read through the documentation but wasn't able to figure this one out. I've thought of 'sloppy' ways to do this with 2 urls.py files, but I know there must be a better way.

You can include a URLs file with an empty pattern. You could do this:

main urls.py

urlpatterns = patterns('',
    (r'foo/', 'foo_view'),
    (r'^', include('myproject.myapp.urls')),
)

app urls.py

urlpatterns = patterns('puzzlequest.pq.views',
    (r'^p/(?P<object_id>\d+)/$', 'some_view'),
    (r'^s/(?P<object_id>\d+)/$', 'other_view'),
)

Note that other routes (like foo/ ) have to come first.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM