转自:http://blog.csdn.net/kevin6216/article/details/6908027
目录(?)[-]
应用程序基于多个django站点,应该考虑到URLConfig包含【include】的方式来处理urls。
from django.conf.urls.defaults import *
urlpatterns = patterns('',
(r'^weblog/', include('mysite.blog.urls')),
(r'^photos/', include('mysite.photos.urls')),
(r'^about/$', 'mysite.views.about'),
)
重要的地方:指向 include() 的正则表达式并不包含一个 $ (字符串结尾匹配符),但是包含了一个斜杆。每当 Django 遇到 include() 时,它将截断匹配的URL,并把【剩余】的字符串发往被包含的 URLconf 作进一步处理。
被包含的 URLconf mysite.blog.urls :
from django.conf.urls.defaults import *
urlpatterns = patterns('',
(r'^(\d\d\d\d)/$', 'mysite.blog.views.year_detail'),
(r'^(\d\d\d\d)/(\d\d)/$', 'mysite.blog.views.month_detail'),
)
当请求:/weblog/2011/ 时,首先是 (r'^weblog/', include('mysite.blog.urls'))匹配/weblog/ , 由于使用include,正则停止匹配,并将 2011/发送到mysite.blog.urls中;
(r'^(\d\d\d\d)/$', 'mysite.blog.views.year_detail') 可以匹配 2011/ ,于是将执行 mysite.blog.views.year_detail 视图,并传递参数 2011
)