包含其他的URLconfs:

在任何时候,你的urlpatterns都可以包含其他URLconf模块。这实际上将一部分URL放置于其他URL下面。

例如,下面是Django 网站自己的URLconf 中一个片段.它包含许多其他URLconf:

node2:/exam/mysite/mysite#cat urls.py
"""mysite URL Configuration

The `urlpatterns` list routes URLs to views. For more information please see:
https://docs.djangoproject.com/en/1.11/topics/http/urls/
Examples:
Function views
1. Add an import: from my_app import views
2. Add a URL to urlpatterns: url(r'^$', views.home, name='home')
Class-based views
1. Add an import: from other_app.views import Home
2. Add a URL to urlpatterns: url(r'^$', Home.as_view(), name='home')
Including another URLconf
1. Import the include() function: from django.conf.urls import url, include
2. Add a URL to urlpatterns: url(r'^blog/', include('blog.urls'))
"""
from django.conf.urls import url,include
from django.contrib import admin

urlpatterns = [
url(r'^polls/', include('polls.urls', namespace='polls')),
url(r'^books/', include('polls.urls', namespace='polls')),
url(r'^admin/', admin.site.urls),
]



from django.conf.urls import include, url

urlpatterns = [
# ... snip ...
url(r'^community/', include('django_website.aggregator.urls')),
url(r'^contact/', include('django_website.contact.urls')),
# ... snip ...
]

注意,这个例子中的正则表达式没有包含$(字符串结束匹配符),但是包含一个末尾的斜杠。

每当Django遇到include()时,它会去掉URL中匹配的部分并将剩下的字符串发送给包含的URLconf做进一步处理。

另外一种包含其他URL模式的方式是使用一个url()实例的列表
url(r'^contact/', include('polls.urls',namespace='aaa')),


node2:/exam/mysite/mysite#cat urls.py
"""mysite URL Configuration

The `urlpatterns` list routes URLs to views. For more information please see:
https://docs.djangoproject.com/en/1.11/topics/http/urls/
Examples:
Function views
1. Add an import: from my_app import views
2. Add a URL to urlpatterns: url(r'^$', views.home, name='home')
Class-based views
1. Add an import: from other_app.views import Home
2. Add a URL to urlpatterns: url(r'^$', Home.as_view(), name='home')
Including another URLconf
1. Import the include() function: from django.conf.urls import url, include
2. Add a URL to urlpatterns: url(r'^blog/', include('blog.urls'))
"""
from django.conf.urls import url,include
from django.contrib import admin

urlpatterns = [
url(r'^polls/', include('polls.urls', namespace='polls')),
url(r'^books/', include('polls.urls', namespace='polls')),
url(r'^admin/', admin.site.urls),
url(r'^contact/', include('polls.urls',namespace='aaa')),
]



url(r'^change/(?P<year>[0-9]{4})/$', views.change),


node2:/exam/mysite/polls#cat urls.py
from django.conf.urls import url,include

from . import views

urlpatterns = [
url(r'^$', views.index, name='index'),
# ex: /polls/5/
url(r'^(?P<question_id>[0-9]+)/$', views.detail, name='detail'),
# ex: /polls/5/results/
url(r'^(?P<question_id>[0-9]+)/results/$', views.results, name='results'),
# ex: /polls/5/vote/
url(r'^(?P<question_id>[0-9]+)/vote/$', views.vote, name='vote'),

url(r'^articles/2003/$', views.special_case_2003),
url(r'^articles/(?P<year>[0-9]{4})/$', views.year_archive),
url(r'^articles/(?P<year>[0-9]{4})/(?P<month>[0-9]{2})/$', views.month_archive),
url(r'^articles/(?P<year>[0-9]{4})/(?P<month>[0-9]{2})/(?P<day>[0-9]{2})/$', views.article_detail),

url(r'^articles/page(?P<num>[0-9]?)/$', views.page),

url(r'^change/(?P<year>[0-9]{4})/$', views.change),


]



<WSGIRequest: GET '/contact/change/1122/'>
1122
Internal Server Error: /contact/change/1122/
Traceback (most recent call last):
File "/usr/local/python27/lib/python2.7/site-packages/django/core/handlers/exception.py", line 41, in inner
response = get_response(request)
File "/usr/local/python27/lib/python2.7/site-packages/django/core/handlers/base.py", line 198, in _get_response
"returned None instead." % (callback.__module__, view_name)
ValueError: The view polls.views.change didn't return an HttpResponse object. It returned None instead.
[18/Aug/2018 21:04:33] "GET /contact/change/1122/ HTTP/1.1" 500 56611