具有不同根的 Django URL 模式

发布于 2024-10-12 12:05:51 字数 751 浏览 3 评论 0原文

我有两个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

老街孤人 2024-10-19 12:05:51

您可以包含具有空模式的 URL 文件。你可以这样做:

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+)/

请注意,其他路由(如 foo/)必须首先出现。

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

请注意,其他路由(如 foo/)必须首先出现。

, 'other_view'), )

请注意,其他路由(如 foo/)必须首先出现。

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+)/

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

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

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

, 'other_view'), )

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文