2.Django路由规则
路由规则
1.基于正则的url
在templates目录下创建index.html、detail.html文件
(1)index.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> {% for k,value in user_dict.items %} <li><a href="/detail-{{ k }}.html" target="_blank">{{ value.name }}</a></li> {% endfor %} </body> </html>
(2)idetail.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <h2>详细信息</h2> <h4>用户名:{{ detail_info.name }}</h4> <h4>邮箱:{{ detail_info.email }}</h4> </body> </html>
(3)views
from django.shortcuts import render,HttpResponse USER_DICT = { '1':{'name':'root1','email':'root1@xxx.com'}, '2':{'name':'root2','email':'root2@xxx.com'}, '3':{'name':'root3','email':'root3@xxx.com'}, '4':{'name':'root4','email':'root4@xxx.com'}, } def index(request): return render(request,'index.html',{'user_dict':USER_DICT}) def detail(request,nid): detail_info = USER_DICT[nid] return render(request, "detail.html", {"detail_info": detail_info})
(4)urls
from django.conf.urls import url from django.contrib import admin from cmdb import views urlpatterns = [ url(r'^admin/', admin.site.urls), url(r'^index/', views.index), url(r'^detail-(\d+).html/', views.detail), ]
2.正则url分组
(1)url修改
urlpatterns = [ url(r'^admin/', admin.site.urls), url(r'^index/', views.index), url(r'^detail-(?P<nid>\d+)-(?P<uid>\d+).html/', views.detail), ]
(2)views修改
url中是一个字典的格式,分组对应 {'nid':'\d+','uid':'\d+'}
def detail(request,**kwargs): print(kwargs) # {'nid':1,'uid':4} nid = kwargs.get('nid') detail_info = USER_DICT[nid] return render(request, "detail.html", {"detail_info": detail_info})
(3)login中也要对应
{% for k,value in user_dict.items %} <li><a href="/detail-{{ k }}-4.html" target="_blank">{{ value.name }}</a></li> {% endfor %}
3.request.path_info获取当前客户端访问的路劲
(1)urls
from django.conf.urls import url,include from django.contrib import admin from cmdb import views urlpatterns = [ url(r'^index', views.index), ]
(2)views
def index(request): print(request.path_info) #获取客户端当前的访问链接 # / index return render(request,"index.html",{"user_dict":USER_DICT})
(3)inde.html
==> action="{{ request.path_info }}
<form action="{{ request.path_info }}" method="post"> <p><input name="user" type="text" placeholder="用户名"/></p> <p><input name="password" type="password" placeholder="密码"/></p> <p><input type="submit" value="提交"/></p> </form>
4.name
对路由关系进行命名,根据此名称生成自己想要的URL
views
def index(request,*args,**kwargs): return render(request,"index.html")
urls
from django.conf.urls import url from django.contrib import admin from cmdb import views urlpatterns = [ # url(r'^first/', views.index, name='i1'), #第一种 # url(r'^second/(\d+)/(\d+)/', views.index,name='i2'), #第二种 url(r'^third/(?P<pid>\d+)/(?P<nid>\d+)/',views.index,name='i3'), #第三种 ]
index.html
第一种 <form action="{% url 'i1' %}" method="post"> 第二种 <form action="{% url 'i2' 1 2 %}" method="post"> 第三种 <form action="{% url "i3" pid=1 nid=9 %}" method="post"> <p><input name="user" type="text" placeholder="用户名"/></p> <p><input name="password" type="password" placeholder="密码"/></p> <p><input type="submit" value="提交"/></p> </form>
5.路由分发
主程序urls.py
from django.conf.urls import url,include from django.contrib import admin from cmdb import views urlpatterns = [ url(r'^admin/', admin.site.urls), url(r'^cmdb/', include('cmdb.urls')), ]
cmdb下的urls.py
from django.conf.urls import url from django.contrib import admin from cmdb import views urlpatterns = [ url(r'^time/',views.time), ]
访问: http://127.0.0.1:8000/cmdb/time/
posted on 2018-01-25 16:09 zhang_derek 阅读(3148) 评论(0) 编辑 收藏 举报