1. 对于login.html 此为跳转文件, 加入了参数nid,在views.py中进行关于request.POST.get()的文件中获取
<a href='/detail?nid={{k}}'>
对于url.py
url(r'detail/', views.detail)
对于
login.html
<a href='/detail/?nid={{k}}>
对于views.detail
def detail(request):
nid = request.GET.get('nid')
def detail(request, nid): User_dict = User_list(nid) return render(request, 'detail.html', {'user_dict':User_dict})
对于detail.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <h1>用户详情</h1> <ul> <p> 用户名: {{user_detail.name}} </p> <p> 邮箱: {{user_detail.email}} </p> </ul> </body> </html>
2. django 进行区分,直接将参数传递给views,由nid接收
对于login.html
<a href='/detail-{{k}}.html'>
对于url.py
url(r'detail-(\d+).html', views.detail)
对于views.py
def detail(request, nid): User_dict = User_list(nid) return render(request, 'detail.html', {'user_dict':User_dict})
对于detail.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <h1>用户详情</h1> <ul> <p> 用户名: {{user_detail.name}} </p> <p> 邮箱: {{user_detail.email}} </p> </ul> </body> </html>
3. 这里对于指定传输的数据进行参数指定
对于login.html
<a href='/detail-{{k}}-2.html'>
对于url.py 指定参数nid和uid
url(r'^detail-(?P<nid>\d+)-(?P<uid>\d+).html', views.detail),
对于views.py
def detail(request, nid, uid): # nid = request.GET.get('nid') # print(nid) User_detail = User_list[nid] print(User_detail) return render(request, 'detail.html', {'user_detail': User_detail})
对于detail.py
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <h1>用户详情</h1> <ul> <p> 用户名: {{user_detail.name}} </p> <p> 邮箱: {{user_detail.email}} </p> </ul> </body> </html>
对于上述的情况,我们在接收参数时,对于
url(r'detail-(d+\).html', views.detail)时
views.detail文件可以写成
def detail(request, *args)
对于
url(r'detail-(?P<nid>\d+).html', views.detail)
views.detail文件可以写成
def detail(request, **kwargs)
4. 设置name
通过name获取
一。指定路径
url.py
url(r'^sdaddad/(\d+)/', views.detail, name='indexx'),
index.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <ul> {% for k, v in user_list.items %} {# <a href="/detail/?nid={{k}}">#} <a href={% url 'indexx' 2%}> <li>{{ k }}</li> </a> {% endfor %} </ul> </body> </html>
views.py
def detail(request, nid):
# nid = request.GET.get('nid')
# print(nid)
User_detail = User_list[nid]
print(User_detail)
return render(request, 'detail.html', {'user_detail': User_detail})
二.返回当前文本
request.path_info
5. reverse 与urls中的name=’indexx‘
from django.urls import reverse
def detail(request):
reverse('indexx')
reverse('index', args=(1, ))
reverse('index', kwargs={pid:1, nid:2 })