24. Django routing rules

24. Django routing rules

Routing rules

1. Based on regular url

Create index.html and detail.html files in the templates directory

(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>Detailed information</h2>
    <h4>User name: {{ detail_info.name }}</h4>
    <h4>Email: {{ 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. Regular URL grouping

(1)URL modification

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 modification

The url is in a dictionary format, and the group corresponds to {'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) Correspondence in 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 gets the path of the current client access

(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) #Get the current access link of the client
    #/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="Username"/></p>
    <p><input name="password" type="password" placeholder="Password"/></p>
    <p><input type="submit" value="Submit"/></p>
</form>

4.name

Name the routing relationship and generate the URL you want based on this name

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'), #first kind
    # url(r'^second/(\d+)/(\d+)/', views.index,name='i2'), #Second
      url(r'^third/(?P<pid>\d+)/(?P<nid>\d+)/',views.index,name='i3'), #The third
]

index.html

        The first
        <form action="{% url'i1' %}" method="post">
        The second
        <form action="{% url'i2' 1 2 %}" method="post">
        The third
        <form action="{% url "i3" pid=1 nid=9 %}" method="post">

            <p><input name="user" type="text" placeholder="Username"/></p>
            <p><input name="password" type="password" placeholder="Password"/></p>
            <p><input type="submit" value="Submit"/></p>
        </form>

 5. Route distribution

Main program 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')),
]

urls.py under cmdb

from django.conf.urls import url
from django.contrib import admin
from cmdb import views

urlpatterns = [
    url(r'^time/',views.time),
]

Visit: http://127.0.0.1:8000/cmdb/time/

Reference: https://cloud.tencent.com/developer/article/1091584 24. Django routing rules-Cloud + Community-Tencent Cloud