第一种场景:
无论url怎么变,表单里面的url始终不变
http://127.0.0.1:8000/CC/indexssssssssssssssssss/
url(r'^indexssssssssssssssssss/', views.index,name='indexx'),
<form action="{% url 'indexx' %}" method="POST">#}
def index(request):
return render(request,"index1.html",{'user_dict':USER_DICT}
第二种场景:
url加ID
http://127.0.0.1:8000/CC/indexssssssssssssssssss/3/
url(r'^indexssssssssssssssssss/(d+)/', views.index,name='indexx'),
<form action="{% url 'indexx' 3 %}" method="POST">
def index(request,nid):
print nid
return render(request,"index1.html",{'user_dict':USER_DICT})
第三种场景显示当前url:(适用于在当前页面提交之后继续留在当前页面)
http://127.0.0.1:8000/CC/indexssssssssssssssssss/33/
url(r'^indexssssssssssssssssss/(d+)/', views.index,name='indexx'),
<form action="{{request.path_info}}" method="POST">#}
def index(request,nid):
print request.path_info
return render(request,"index1.html",{'user_dict':USER_DICT,})
对URL路由关系进行命名, ***** 以后可以根据此名称生成自己想要的URL *****
url(r'^asdfasdfasdf/', views.index, name='i1'),
url(r'^yug/(d+)/(d+)/', views.index, name='i2'),
url(r'^buy/(?P<pid>d+)/(?P<nid>d+)/', views.index, name='i3'),
#反转生成url
def func(request, *args, **kwargs):
from django.urls import reverse
url1 = reverse('i1') # asdfasdfasdf/
url2 = reverse('i2', args=(1,2,)) # yug/1/2/
url3 = reverse('i3', kwargs={'pid': 1, "nid": 9}) # buy/1/9/
xxx.html
{% url "i1" %} # asdfasdfasdf/
{% url "i2" 1 2 %} # yug/1/2/
{% url "i3" pid=1 nid=9 %} # buy/1/9/
注:
# 当前的URL