django url 正则表达式收集
url(r'^about/$',views.about),
url(r'^list/$',views.listing),
只匹配:
localhost:8000/about
localhost:8000/about/
localhost:8000/list
localhost:8000/list/
由于是加上了 $ 符号,所有,不能匹配:
localhost:8000/about/xyz
localhost:8000/about/1
输入localhost:8000/about 的时候,会自动加入 /
变成localhost:8000/about/
url(r'^about/[0|1|2|3]/$',views.about),
localhost:8000/about/0
localhost:8000/about/1
localhost:8000/about/2
localhost:8000/about/3
url(r'^about/([0|1|2|3])/$',views.about),
def about(request,author_no):
html = "Here is Author:{}'s about page!
".format(author_no)
return HttpResponse(html)
url(r'^about/(?P[0|1|2|3])/$' ,views.about),
def about(request,author_no):
html = "Here is Author:{}'s about page!
".format(author_no)
return HttpResponse(html)
localhost:8000/list/2016/05/12
localhost:8000/post/2016/05/12/01
url(r'^list/(?P\d{4}/\d{1,2}/\d{1,2})$' ,views.listing),
url(r'^post/(?P\d{4}/\d{1,2}/\d{1,2}/\d{1,3})$' , views.post),
def listing(request,list_date):
html = "List Date is {}
".format(list_date)
return HttpResponse(html)
def post(request,post_data):
html = "Post Data is {}
".format(post_data)
return HttpResponse(html)
url(r'^post/(\d{4})/(\d{1,2})/(\d{1,2})/(\d{1,3})$',views.post),
def post(request,yr,mon,day,post_num):
html = "{}/{}/{}:Post Number:{}
".format(yr,mon,day,int(post_num))
return HttpResponse(html)
url(r'^about/$',views.about),
url(r'^about/(?P[0|1|2|3])/$' ,views.abot),
def about(request, author_no = '0'):
html = "Here is Author:{}'s about page !
".format(author_no)
return HttpResponse(html)
url(r'^$',views.homepage,{'testmode':'YES'}),
def homepage(request, testmode):
html = "Here is mode:{} homepagepage !
".format(testmode)
return HttpResponse(html)
(稍后补充)