django url 正则表达式收集
1
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/
2
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
3
url(r'^about/([0|1|2|3])/$',views.about),
def about(request,author_no):
html = "<h2>Here is Author:{}'s about page!</h2><hr>".format(author_no)
return HttpResponse(html)
4
url(r'^about/(?P<author_no>[0|1|2|3])/$',views.about),
def about(request,author_no):
html = "<h2>Here is Author:{}'s about page!</h2><hr>".format(author_no)
return HttpResponse(html)
5
localhost:8000/list/2016/05/12
localhost:8000/post/2016/05/12/01
url(r'^list/(?P<list_date>\d{4}/\d{1,2}/\d{1,2})$',views.listing),
url(r'^post/(?P<post_data>\d{4}/\d{1,2}/\d{1,2}/\d{1,3})$', views.post),
def listing(request,list_date):
html = "<h2>List Date is {} </h2><hr>".format(list_date)
return HttpResponse(html)
def post(request,post_data):
html = "<h2>Post Data is {} </h2><hr>".format(post_data)
return HttpResponse(html)
6
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 = "<h2>{}/{}/{}:Post Number:{}</h2><hr>".format(yr,mon,day,int(post_num))
return HttpResponse(html)
7
url(r'^about/$',views.about),
url(r'^about/(?P<author_no>[0|1|2|3])/$',views.abot),
def about(request, author_no = '0'):
html = "<h2>Here is Author:{}'s about page !</h2><hr>".format(author_no)
return HttpResponse(html)
8
url(r'^$',views.homepage,{'testmode':'YES'}),
def homepage(request, testmode):
html = "<h2>Here is mode:{} homepagepage !</h2><hr>".format(testmode)
return HttpResponse(html)
(稍后补充)