1、非命名传参
修改urls.py
re_path(r'^wushen/', views.wushen, name='list'),
修改views.py
def wushen(request):
reverse('list')
return render(request, 'add.html')
def wushenadd(request):
return HttpResponse('wushenadd')
修改add.html
<a href="{% url 'add' %}">1</a> <a href="{% url 'add' %}">2</a> <a href="{% url 'add' %}">3</a> <a href="{% url 'add' %}">4</a> <a href="{% url 'add' %}">5</a> <a href="{% url 'add' %}">6</a> <a href="{% url 'add' %}">7</a>
位置传参
修改urls.py
re_path(r'^wushen/(/d+)/', views.wushen, name='list'), re_path(r'^wushenadd/(/d+)/', views.wushenadd, name='add'),
修改views.py
def wushen(request,xx): reverse('list',args=(1,)) return render(request, 'add.html') def wushenadd(request,xx): return HttpResponse('wushenadd')
修改add.html
def wushen(request,xx): reverse('list',args=(1,)) return render(request, 'add.html') def wushenadd(request,xx): return HttpResponse('wushenadd')
命名传参
修改urls.py
re_path(r'^wushen/(?P<year>/d+)/', views.wushen, name='list'), re_path(r'^wushenadd/(?P<a>/d+)/', views.wushenadd, name='add'),
修改views.py
def wushen(request,year): reverse('list',args=(1,)) return render(request, 'add.html') def wushenadd(request,a): return HttpResponse('wushenadd')
修改index.html
<a href="{% url 'add' 1 %}">1</a> <a href="{% url 'add' 1 %}">2</a> <a href="{% url 'add' 1 %}">3</a> <a href="{% url 'add' 1 %}">4</a> <a href="{% url 'add' 1 %}">5</a> <a href="{% url 'add' 1 %}">6</a>
原创文章,作者:jamestackk,如若转载,请注明出处:https://blog.ytso.com/245433.html