Django将所有未捕获的URL路由到包含的urls.py [英] Django route all non-catched urls to included urls.py
本文介绍了Django将所有未捕获的URL路由到包含的urls.py的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我希望每个不以"api"开头的网址都使用foo/urls.py
I want every url that does not start with 'api' to use the foo/urls.py
urls.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
foo/urls.py
from django.conf.urls import include, url
from foo import views
urlpatterns = [
url(r'a$', views.a),
]
这行不通,知道吗?
推荐答案
如果您想捕获所有网址格式,请使用:
If you want a catch all url pattern, use:
url(r'^', include(foo_urls)),
来自文档:
每当Django遇到
include()
时,它都会截断直到该点匹配的URL的任何部分,并将剩余的字符串发送到包含的URLconf中以进行进一步处理.
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
.
In your current code, the regex ^.*/$
matches the entire url /a/
. This means that there is nothing remaining to pass to foo_urls
.
这篇关于Django将所有未捕获的URL路由到包含的urls.py的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文