gpt4 book ai didi

python - Django 将所有未捕获的 url 路由到包含的 urls.py

转载 作者:太空狗 更新时间:2023-10-30 00:29:31 24 4
gpt4 key购买 nike

我希望每个不以“api”开头的 url 都使用 foo/urls.py

网址.py

from django.conf.urls import include, url
from foo import urls as foo_urls

urlpatterns = [
url(r'^api/', include('api.urls', namespace='api')),
url(r'^.*/$', include(foo_urls)),
]

foo/urls.py

from django.conf.urls import include, url
from foo import views

urlpatterns = [
url(r'a$', views.a),
]

这行不通,知道吗?

最佳答案

如果你想要捕获所有 url 模式,请使用:

url(r'^', include(foo_urls)),

来自 the docs :

Whenever Django encounters include() it chops off whatever part of the URL matched up to that point and sends the remaining string to the included URLconf for further processing.

在您当前的代码中,正则表达式 ^.*/$ 匹配整个 url /a/。这意味着没有任何东西可以传递给 foo_urls

关于python - Django 将所有未捕获的 url 路由到包含的 urls.py,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31248126/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com