Django学习手册 - reverse()反转URL

时间:2021-09-26 15:37:06

前端:

    <h1>测试</h1>
<a href="/ce_test/?id=1">1按键</a>
<a href="/ce_test/?id=2">2按键</a>
<a href="/ce_test/?id=3">3按键</a>
<a href="/ce_test/?id=4">4按键</a>
<a href="/ce_test/?id=5">5按键</a>

URL:

    #访问的URL
url(r"^ce_test/$",views.ce_test), #跳转的URL
# 元组传值
# url(r"^caxun/(\d+)/$",views.caxun,name='caxun'), # 字典传值
url(r"^eeeee/(?P<id>\d+)$",views.caxun,name='caxun'),

views:

跳转前的页面:

def ce_test(request):

    if request.method == "GET":
if request.GET.get('id',None):
id = request.GET.get('id')
# 元组传值
# login_url = reverse('caxun',args=(1,)) # 字典传递至
login_url = reverse('caxun',kwargs={'id':id}) return redirect(login_url)
else:
return render(request,'ce_test.html')

跳转的页面:

# 以元组的方式获取值:
def caxun(request,num): #以字典的方式获取值:
def caxun(request,**kwargs):
print(kwargs)
num = kwargs['id']