Django之路由系統


一、路由系統介紹

在django程序中,可以通過urls.py文件對所有的url進行任務的分配,根據路由規則的定義選擇不同的業務處理函數進行處理

二、路由規則定義

1、路由規則代碼如下,mysite/mysite/urls.py

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


urlpatterns = [

    #####靜態路由#####
    # ① 匹配規則  http://127.0.0.1:8000/index/*
    url(r'^index/', views.index),

    #####動態路由--利用正則表達式可以達到分頁url的效果#####
    # ② 匹配規則  http://127.0.0.1:8000/detail/432432  將最后面的數字當做參數傳遞給views.detail函數的nid參數
    url(r'^detail/(\d+)', views.detail),

    # ③ 匹配規則  http://127.0.0.1:8000/detail2/432432/2  將最后面的兩個數字當做參數分別傳遞給views.detail函數的nid和nnid參數
    url(r'^detail2/(\d+)/(\d+)', views.detail2),

    # ④ 匹配規則  http://127.0.0.1:8000/detail3/432432/2  將最后面的兩個數字根據自定義的名字當做參數分別傳遞給views.detail函數的p1和p2參數
    url(r'^detail3/(?P<p1>\d+)/(?P<p2>\d+)', views.detail3),
    
    #####路由分發#####
    # 當一個網站變得龐大之后,在一個project中就會存在很多的路由規則,可以使用路由分發將每個APP的路由規則分發至APP自己的路由規則進行處理
    # 通過include可以將以web開頭所有的url都分發給web.urls中的路由去進行處理
    url(r'^web/', include("web.urls")),
]

2、業務處理函數的代碼如下, mysite/cmdb/views.py

from django.shortcuts import render
from django.shortcuts import HttpResponse

# Create your views here.


def index(request):
    return render(request, "index.html")


def detail(request, nid):
    print(nid)
    return HttpResponse("OK")


def detail2(request, nid, nnid):
    print(nid, nnid)
    return HttpResponse("OK")


def detail3(request, p1, p2):
    print(p1, p2)
    return HttpResponse("OK")

3、程序目錄結構

mysite/
├── cmdb
│   ├── admin.py
│   ├── apps.py
│   ├── __init__.py
│   ├── migrations
│   │   └── __init__.py
│   ├── models.py
│   ├── tests.py
│   └── views.py
├── db.sqlite3
├── manage.py
├── mysite
│   ├── __init__.py
│   ├── settings.py
│   ├── urls.py
│   └── wsgi.py
├── static
│   └── s1.css
└── templates
    └── index.html

三、動態路由的應用 -- 實現分頁和詳細信息的功能

1、路由規則代碼 mysite/mysite/urls.py

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


urlpatterns = [
    url(r'^index/(\d+)/', views.index),     # 分頁
    url(r'^detail/(\d+)/', views.detail),   # 詳細信息
]

2、業務處理函數代碼 mysite/cmdb/views.py

from django.shortcuts import render
from django.shortcuts import HttpResponse

# Create your views here.

# 臨時存放一些數據,生產環境中,這些數據都是保存在數據庫中
USER_LIST = []
for item in range(108):
    temp = {"id": item, "username": "name"+str(item), "email": "email"+str(item)}
    USER_LIST.append(temp)


def index(request, page):
    # 將用戶信息分頁展示

    print(page)
    # 第一頁  0-9
    # 第二頁  10-19
    # 第三頁  20-29
    page = int(page)
    start_id = (page - 1) * 10
    end_id = page * 10 -1

    user_list = USER_LIST[start_id:end_id]

    return render(request, "index.html", {"user_list": user_list})


def detail(request, nid):
    # 用戶ID的詳細信息
    nid = int(nid)
    current_detail_dict = USER_LIST[nid]
    return render(request, "detail.html", {"current_detail_dict": current_detail_dict})

3、分頁html代碼 mysite/templates/index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link rel="stylesheet" href="/static/s1.css">
</head>
<body>

    <table>
        <thead>
            <tr>
                <td>ID</td>
                <td>用戶名</td>
                <td>詳細</td>
            </tr>
        </thead>
        <tbody>
            {% for item in user_list %}

                <tr>
                    <td>{{ item.id }}</td>
                    <td>{{ item.username }}</td>
                    <td><a href="/detail/{{ item.id }}/" target="_blank">查看詳細</a></td>
                </tr>
            {% endfor %}

        </tbody>

    </table>


</body>
</html>

4、用戶詳細信息html代碼 mysite/templates/detail.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link rel="stylesheet" href="/static/s1.css">
</head>
<body>

    <ul>
        <li>{{ current_detail_dict.id }}</li>
        <li>{{ current_detail_dict.username }}</li>
        <li>{{ current_detail_dict.email }}</li>

    </ul>


</body>
</html>

5、目錄結構

mysite/
├── cmdb
│   ├── admin.py
│   ├── apps.py
│   ├── __init__.py
│   ├── migrations
│   │   └── __init__.py
│   ├── models.py
│   ├── tests.py
│   └── views.py
├── db.sqlite3
├── manage.py
├── mysite
│   ├── __init__.py
│   ├── settings.py
│   ├── urls.py
│   └── wsgi.py
├── static
│   └── s1.css
└── templates
    ├── detail.html
    └── index.html

6、訪問用戶信息分頁url,點擊頁面查看詳細

  • 通過訪問url http://127.0.0.1:8000/index/3/ 最后的數字可以換成其他的

  • 點擊用戶信息分頁中的對應用戶信息中的查看詳細


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM