Django路由规则

  • 原创
  • 发布时间: 2周前
  • 收藏数: 0 / 点赞数: 0 / 阅读数: 15

基于正则的url


  • 在templates目录下创建index.html、detail.html文件
<!-- file: 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>
<!-- detail.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>
  • 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})
  • 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),
]

 

正则url分组


  • 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),    
]
  • 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})
  • login中也要对应
 {% for k,value in user_dict.items %}
        <li><a href="/detail-{{ k }}-4.html" target="_blank">{{ value.name }}</a></li>
 {% endfor %}

 

request.path_info获取当前客户端访问的路径


  • urls
from django.conf.urls import url,include
from django.contrib import admin
from cmdb import views

urlpatterns = [
    url(r'^index', views.index),
]
  • views
def index(request):
    print(request.path_info)    #获取客户端当前的访问链接
    # / index
    return render(request,"index.html",{"user_dict":USER_DICT})
  • index.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>

 

命名路由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>

 

路由分发


  • 主程序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/

评论