?
?
有名、無名分組
分組正則表達式,加小括號
url(r'^test/(\d+)', views.test),def test(request,xx):print(xx)return HttpResponse("OK")
無名分組
將括號正則表達式匹配的內容,當作位置參數傳遞到后面的視圖函數
?
有名分組
正則表達式起一個別名
url(r'^testadd/(?P<year>\d+)', views.testadd),def testadd(request,year):print(year)return HttpResponse("OK")
將括號正則表達式匹配的內容,當作關鍵字參數傳遞到后面的視圖函數
?
a.無名有名混用,不能混用
b.同以分組可以多次使用
def testadd(request,*arg,**kwargs):print(args)return HttpResponse("OK")
?
反向解析
1.起別名,不能沖突
url(r'^testadd/', views.testadd,name='ooo'),
2.反向解析
a.前端
<a href="{% url 'ooo' %}">test</a>
b. 后端
from .... import render, reverseprint(reverse('ooo'))
?
無名分組反向解析
url(r'^edit/(\d+)', views.edit,name='xxx'),def edit(request,edit_id):reverse('xxx',args=(edit_id,)){%for user_obje in user_queryset%}<a href='{% url 'xxx' user_obj.id %}'>編輯</a>
{%endfor%}
?
有名分組反向解析
url(r'^func/(?P<year>\d+)/', views.func,name='ooo'),#前端#寫法1,了解print(reverse('ooo',kwargs={'year':123}))#簡單寫法,與無名一樣print(reverse('ooo',args=(111,)))#前端<a href='{% url 'ooo' year=123 %}'>1111</a> 了解即可<a href='{% url 'ooo' 123 %}'>1111</a> 記憶
Q:多個參數?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?