Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django route all non-catched urls to included 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

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

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

This does not work, any idea ?

like image 728
yossi Avatar asked Jul 06 '15 14:07

yossi


1 Answers

If you want a catch all url pattern, use:

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

From 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.

In your current code, the regex ^.*/$ matches the entire url /a/. This means that there is nothing remaining to pass to foo_urls.

like image 198
Alasdair Avatar answered Sep 20 '22 23:09

Alasdair