#url.py
urlpatterns = patterns('',
(r'^$', views.homepage),
(r'^(\d{4})/([a-z]{3})/$', views.archive_month),
)
if settings.DEBUG:
urlpatterns += patterns('',
(r'^debuginfo/$', views.debug),
)
注意:
patterns()返回的对象是可相加的
URL链接/debuginfo/ 只在你的DEBUG 配置项设为True 时才有效。
-----------------------------------------
#url.py
urlpatterns = patterns('mysite.views',#公共前缀
(r'^hello/$', 'hello'),#注意这里hello两边的引号
(r'^time/$', 'current_datetime'),
(r'^time/plus/(\d{1,2})/$', 'hours_ahead'),
)
urlpatterns += patterns('weblog.views',
(r'^tag/(\w+)/$', 'tag'),
)
注意:
当使用公共前缀的时候视图函数需要加单引号(比如:hello)
-----------------------------------------
下边是使用无命名组的URLconf的例子:
#url.py
from django.conf.urls.defaults import *
from mysite import views
urlpatterns = patterns('',
(r'^articles/(\d{4})/$', views.year_archive),
(r'^articles/(\d{4})/(\d{2})/$', views.month_archive),
)
下面是相同的 URLconf,使用命名组进行了重写:
#url.py
from django.conf.urls.defaults import *
from mysite import views
urlpatterns = patterns('',
(r'^articles/(?P<year>\d{4})/$', views.year_archive),
(r'^articles/(?P<year>\d{4})/(?P<month>\d{2})/(?P<day>\d{2})/$', views.month_archive),
)
↑
这段代码和前面的功能完全一样,只有一个细微的差别: 取的值是以关键字参数的方式而不是以位置参数的方式传递给视图函数的。
也就是说使用命名组可以让你的URLconfs更加清晰,减少搞混参数次序的潜在BUG,还可以让你在函数定义中对参数重新排序。
在 Python 正则表达式中,命名的正则表达式组的语法是 (?P<name>pattern) ,这里name 是组的名字,而pattern 是匹配的某个模式。
注意:
命名组和非命名组不能同时存在于同一个URLconf的模式中,会出现超出预期的匹配,你不会喜欢的
对应有命名组的views.py:
# views.py
import datetime
def day_archive(request, year, month, day):
# The following statement raises a TypeError!
date = datetime.date(year, month, day)
-----------------------------------------
#url.py
urlpatterns = patterns('',
(r'^foo/$', views.foobar_view, {'template_name': 'template1.html'}),
(r'^bar/$', views.foobar_view, {'template_name': 'template2.html'}),#注意和views.py里面的参数传递区分
)
# views.py
from django.shortcuts import render_to_response
from mysite.models import MyModel
def foobar_view(request, template_name):
m_list = MyModel.objects.filter(is_new=True)
return render_to_response(template_name, {'m_list': m_list})#注意和url.py里面的不同模版渲染区分
注意:
URLconf会指定一个template_name,而视图会把template_name当作一个参数
包含其他URLconf
直接看例子
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()的regex没有结束符‘$’,但是有‘/’
Django自带的admin模块就是如此
url(r'^admin/', include(admin.site.urls)),
继续看这个例子,下边就是被包含的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'),
)
通过这两个URLconf,下边有几个例子帮助理解: 直接看英文吧,好理解
★/weblog/2007/: In the first URLconf, the patternr'^weblog/'matches. Because it is aninclude(), Django strips all the matchingtext, which is'weblog/' in this case. The remaining part of the URLis2007/, which matches the first line in themysite.blog.urlsURLconf.
余下的“2007/”先和mysite.blog.urls URLconf里面的第一行匹配,匹配上了就不看下一条了
★/weblog//2007/ (with two slashes): In the first URLconf, the patternr'^weblog/' matches. Because it is an include(), Django stripsall the matching text, which is'weblog/' in this case. The remainingpart of the URL is/2007/ (with a leading slash), which does notmatch any of the lines in themysite.blog.urls URLconf.
余下的“/2007/”并不能和mysite.blog.urls中的两条匹配
★/about/: This matches the viewmysite.views.about in the firstURLconf, demonstrating that you can mixinclude() patterns withnon-include() patterns.
这个直接和第一层URLconf匹配
捕捉的参数如何和include()协同工作
例如:
# root urls.py
from django.conf.urls.defaults import *
urlpatterns = patterns('',
(r'^(?P<username>\w+)/blog/', include('foo.urls.blog')),
)
# foo/urls/blog.py
from django.conf.urls.defaults import *
urlpatterns = patterns('',
(r'^$', 'foo.views.blog_index'),
(r'^archive/$', 'foo.views.blog_archive'),
)
这个例子中,被捕获的username将传递给被包含的URLconf,进而传递给那个URLconf中的每个视图函数(慢慢理解)
注意,这个参数会被传递到被包含URLconf中的每一行
额外参数(附加的dict)如何和include()协同工作
你可以传递相应的URLconf选项到include(),就像你通过字典传递额外的选项到视图一样,当你这么做的时候被包含的URLconf每一行都会收到这个额外参数
比如下边两个URLconf在功能上是一样的:
第一个
# urls.py
from django.conf.urls.defaults import *
urlpatterns = patterns('',
(r'^blog/', include('inner'), {'blogid': 3}),
)
# inner.py
from django.conf.urls.defaults import *
urlpatterns = patterns('',
(r'^archive/$', 'mysite.views.archive'),
(r'^about/$', 'mysite.views.about'),
(r'^rss/$', 'mysite.views.rss'),
)
第二个
# urls.py
from django.conf.urls.defaults import *
urlpatterns = patterns('',
(r'^blog/', include('inner')),
)
# inner.py
from django.conf.urls.defaults import *
urlpatterns = patterns('',
(r'^archive/$', 'mysite.views.archive', {'blogid': 3}),
(r'^about/$', 'mysite.views.about', {'blogid': 3}),
(r'^rss/$', 'mysite.views.rss', {'blogid': 3}),
)