django 1.4.5 documentation
The view layer -->The basics 一、 patterns语法格式:
- patterns = pattern('prefix', //prefix一般为空
- url()
- )
url语法格式:
- patterns = pattern('prefix',
- url(regular expression, view function [, optional dictionary[, name]])
- )
optional dictionary是字典形式参数 optional name是named url pattern 二、语法示例:
- urlpatterns = pattern('',
- (r'^articles/2003/$', 'news.views.special_case_2003'),
- (r'^articles/(\d{4})/$', 'news.views.year_archive'),
- (r'^articles/(\d{4})/(\d{2})/$', 'news.views.month_archive'),
- (r'^articles/(\d{4)/(\d{2})/(\d+)/$', 'news.views.article_detail'),
- )
1、括号内的参数在传递给view视图函数时,都是按位置进行传递 2、url请求/articles/2005/03/ 会被第三条记录匹配,Django会调用news.views.month_archive(request, '2005', '03')函数来处理URL请求 3、第一条和第二条记录都能匹配url请求/articles/2003/ , 但是django会使用第一条记录来匹配,因为urlpattern是按顺序进行匹配,只要匹配到就忽略后面的记录。这种形式不是很好,后面会介绍在views视图函数中使用默认参数来处理这种特殊情况 三、命名组: 命名组语法格式: (?P<name>pattern) 其中,name是分组的名称,pattern是用于匹配的正则表达式 urls.py:
- from django.conf.urls import patterns, url, include
-
- urlpatterns = urlpattern('',
- (r'^articles/2003/$', 'news.views.special_case_2003'),
- (r'^articles/(?P<year>\d{4})/$', 'news.views.year_archive'),
- (r'^articles/(?P<year>\d{4})/(?P<month>\d{2})/$', 'news.views.month_archive'),
- (r'^articles/(?P<year>\d{4})/(?P<month>\d{2})/(?P<day>\d{2})/$', 'news.views.article_detail'),
- )
1、django会调用news.views.month_archive(request, year='2005', month='03')来处理URL请求:/articles/2005/03/ ,这种形式比按位置传递可读性更强一些,也不容易出错 四、urlpattern匹配URL请求时捕获的参数,都是以python字符串的形式传递给view视图函数,不管参数是以什么形式被捕获的:
- (r'^articles/(?P<year>\d{4})/$', 'news.views.year_archive'),
尽管urlpattern在匹配时,\d{4}只匹配数字,但是当它把捕获到的年份数字传递给view视图函数时,是以字符串形式传递的。
五、为视图函数指定默认参数:
- #URLconf
- urlpatterns = pattern('',
- (r'^blog/$', 'blog.views.page'),
- (r'^blog/page(?P<num>\d+)/$', 'blog.views.page'),
- )
-
- # View (in blog/views.py)
- def page(request, num="1"):
- #Output the appropriate page of blog entries, according to num.
上述示例,所有的请求都使用blog.views.page视图函数来处理URL请求;但当URL请求仅包括 /blog/ 时(特殊情况),被第一条记录匹配,视图函数page在处理该请求时,自动使用默认参数num的值1,具体形式由函数体定义。 六、简化urlpattern书写: 1、The view prefix:
- urlpatterns = patterns('news.views', //把前缀news.views添加到这里,避免在每一条记录中重复书写
- (r'^articles/(\d{4})/$', 'year_archive'),
- (r'^articles/(\d{4})/(\d{2})/$', 'month_archive'),
- (r'^articles/(\d{4})/(\d{2})/(\d+)/$', 'article_detail'),
- )
2、Including other URLconfs:
- urlpatterns = patterns('',
- # ... snip ...
- (r'^comments/', include('django.contrib.comments.urls')),
- (r'^community/', include('django_website.aggregator.urls')),
- (r'^contact/', include('django_website.contact.urls')),
- (r'^r/', include('django.conf.urls.shortcut')),
- # ... snip ...
- )
使用include时,正则表达式不能使用$,因为在django执行时include时,是把include的url追加到后面 另一种形式:
- from django.conf.urls import patterns, url, include
- extra_patterns = patterns('',
- url(r'^reports/(?P<id>\d+)/$', 'credit.views.report', name='credit-reports'),
- url(r'^charge/$', 'credit.views.charge', name='credit-charge'),
- )
-
- urlpatterns = patterns('',
- url(r'^$', 'apps.main.views.homepage', name='site-homepage'),
- (r'^help/', include('apps.help.urls')),
- (r'^credit/', include(extra_patterns)),
- )
七、传递额外参数到视图函数:
- urlpatterns = patterns('blog.views',
- (r'^blog/(?P<year>\d{4})/$', 'year_archive', { 'foo': 'bar'}),
- )
1、额外参数都是字典形式 2、如果字典参数的key与命名组名称重名,则参数在传递给view视图函数时,字典参数会覆盖urlpattern捕获的参数。如:
- urlpatterns = patterns('blog.views',
- (r'^blog/(?P<year>\d{4})/$', 'year_archive', { 'year': '000'}),
- )
view视图函数在处理URL请求/blog/2013/,使用的参数不是2013,而是000,尽管urlpattern捕获的参数是2013。
八、传递额外参数到include():
- # main.py
- urlpatterns = patterns('',
- (r'^blog/', include('inner'), { 'blogid': 3}),
- )
-
- # inner.py
- urlpatterns = patterns('',
- (r'^archive/$', 'mysite.views.archive'),
- (r'^about/$', 'mysite.views.about'),
- )
相当于:
- # main.py
- urlpatterns = patterns('',
- (r'^blog/', include('inner')),
- )
-
- # inner.py
- urlpatterns = patterns('',
- (r'^archive/$', 'mysite.views.archive', { 'blogid': 3}),
- (r'^about/$', 'mysite.views.about', { 'blogid': 3}),
- )
1、额外参数都是字典形式 2、传递给include()的额外参数,将传递到include里的每一条记录,不管该处理该记录的视图函数是否能正确接受相应的参数 九、Naming URL patterns:
- urlpatterns = patterns('',
- url(r'^archive/(\d{4})/$', archive, name="full-archive"),
- url(r'^archive-summary/(\d{4})/$', archive, { 'summary': True}, "arch-summary"),
- )
使用: {% url arch-summary 1945 %} {% url full-archive 2007 %} 当多条记录都使用同一个视图函数处理时,使用named url来区分不同的记录