具有不同根的 Django URL 模式
我有两个 URL 模式,它们都存在于我正在设置的同一个应用程序中。
我需要像下面这样的网址才能工作。
但是,这两个都位于同一个 django 应用程序中。
我的主 urls.py 看起来像这样,用于处理 /p/12345 url。
urlpatterns = patterns('',
(r'^p/', include('myproject.myapp.urls')),
)
我的应用程序的 urls.py 类似。但这仍然只处理 /p/12345 url。
urlpatterns = patterns('myproject.myapp.views',
(r'^(?P<object_id>\d+)/$', 'some_view'),
)
我的问题是两者几乎相同,只是前缀不同。如何对 /p/12345 和 /s/12345 网址执行此操作。我已通读文档,但无法弄清楚这一点。我想过用 2 个 urls.py 文件来“草率”地做到这一点,但我知道一定有更好的方法。
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+)/
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.
, '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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以包含具有空模式的 URL 文件。你可以这样做:
main urls.py
app urls.py
请注意,其他路由(如
foo/
)必须首先出现。You can include a URLs file with an empty pattern. You could do this:
main urls.py
app urls.py
Note that other routes (like
foo/
) have to come first.