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

时间:2015-07-06 14:12:57

标签: python django url-routing

我希望每个不以#ap;'开头的网址开头。使用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)),
]    

富/ urls.py

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

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

这不起作用,任何想法?

2 个答案:

答案 0 :(得分:11)

如果您想要捕获所有网址格式,请使用:

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

来自the docs

  

每当Django遇到include()时,它会删除与该点匹配的URL的任何部分,并将剩余的字符串发送到包含的URLconf以进行进一步处理。

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

答案 1 :(得分:2)

这样做:

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