django 捕获的值为关键字参数



1.
/articles/2005/03/ 请求将调用views.month_archive(request, year='2005', month='03')函数,

而不是views.month_archive(request, '2005', '03')。

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),
]


def year_archive(req,year):
   print req
   print year
   print req.get_full_path();
   return HttpResponse(req.get_full_path()+'|'+year)

   
http://192.168.137.3:8000/books/articles/2089/

/books/articles/2089/|2089

这个实现与前面示例完全相同,只有一个细微的差别:

捕获的值作为关键字参数而不是位置参数传递给视图函数  
原文地址:https://www.cnblogs.com/hzcya1995/p/13349087.html