开发者

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.

  • http://www.domain.com/p/12345
  • http://www.domain.com/s/12345

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

My main urls.py loo开发者_如何学编程ks 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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜