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")), ]
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")
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
from django.conf.urls import url from cmdb import views urlpatterns = [ url(r'^index/(\d+)/', views.index), # 分頁 url(r'^detail/(\d+)/', views.detail), # 詳細信息 ]
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})
<!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>
<!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>
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
經過訪問url http://127.0.0.1:8000/index/3/ 最後的數字能夠換成其餘的
css
點擊用戶信息分頁中的對應用戶信息中的查看詳細
html