Django學習手冊 - reverse()反轉URL
阿新 • • 發佈:2018-12-27
前端:
<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']