Django 路由層URLconf的實現(xiàn)
分組
分組的目的:讓服務(wù)端獲得url中的具體數(shù)據(jù),通過分組,把需要的數(shù)據(jù)按函數(shù)傳參的方式傳遞給服務(wù)器后臺
1-無名分組
若要從URL 中捕獲一個值,只需要在它周圍放置一對圓括號
# app01/urls.py
from django.urls import path, re_path
from app01 import views
app_name = "app01"
urlpatterns = [
path("login/", views.login, name="Log"),
re_path(r"articles/([0-9]{4})/([0-9]){2}/", views.articles),
]
# app01/views.py def articles(request, year, month ): print(year, month) return HttpResponse(year+"-"+month)
2-有名分組
在更高級的用法中,可以使用命名的正則表達(dá)式組來捕獲URL 中的值并以關(guān)鍵字 參數(shù)傳遞給視圖。
在Python 正則表達(dá)式中,命名正則表達(dá)式組的語法是(?P<name>),其中name 是組的名稱,下面是以上URLconf 使用命名組的重寫。
# app01/urls.py
from django.urls import path, re_path
from app01 import views
app_name = "app01"
urlpatterns = [
path("login/", views.login, name="Log"),
re_path(r"articles/(?P<year>[0-9]{4})/(?P<month>[0-9]{2}/)", views.articles),
]
捕獲的值作為關(guān)鍵字參數(shù)而不是位置參數(shù)傳遞給視圖函數(shù)
# app01/views.py def articles(request, month, year ): print(year, month) return HttpResponse(year+"-"+month)
分發(fā)
分發(fā)的目的:解決一個django項目中因為存在多個應(yīng)用app導(dǎo)致project下面的urls臃腫和分配混亂的問題
分發(fā)的具體操作流程是:
step1:項目文件下的urls.py,使用include()
# project/urls.py
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path(r"app01/", include("app01.urls")),
]
step2:app下的具體url
# app01/urls.py
from django.urls import path
from app01 import views
urlpatterns = [
path("login/", views.login),
]
step3:視圖函數(shù)render時模板路徑前綴
# app01/views.py from django.shortcuts import render # Create your views here. def login(request): return render(request, "app01/login.html")
step4:模板
# app01/templates/app01/login.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>app01_title</title>
</head>
<body>
<h2>app01_login</h2>
<hr>
<form action="" method="post">
<p>用戶名 <input type="text" name="name"></p>
<p>密碼 <input type="password" name="pwd"></p>
<p><input type="submit" value="登錄"></p>
</form>
</body>
</html>
反向解析
反向解析的目的:解決url硬編碼的問題,即不能寫死一個url,否則日后修改url,造成的維護(hù)成本巨大
此時可以給url命名,然后可以在視圖和模板中使用url別名,反向解析出正式的url
反向解析分兩種:模板中解析、視圖中解析
模板中解析 <form action="{% url "Art" 12 %}" method="post">
# app01/urls.py
from django.urls import path
from app01 import views
urlpatterns = [
path("login/", views.login, name="Log"),
path("articles/<int:id>/", views.articles, name="Art"), #有參
]
# app01/templates/app01/login.html
<form action="{% url "Log" %}" method="post">
<p>用戶名 <input type="text" name="name"></p>
<p>密碼 <input type="password" name="pwd"></p>
<p><input type="submit" value="登錄"></p>
</form>
<form action="{% url "Art" 12 %}" method="post">
<p>用戶名 <input type="text" name="name"></p>
<p>密碼 <input type="password" name="pwd"></p>
<p><input type="submit" value="登錄"></p>
</form>
視圖中解析 reverse("Art", args=(id,))
# app01/views.py
from django.shortcuts import render
from django.urls import reverse
# Create your views here.
def login(request):
print(reverse('log'))
id=10
print(reverse("Art", args=(id,)))
return render(request, "app01/login.html")
命名空間
命名空間要配合反向解析使用,當(dāng)存在多個app,url的name沖突時,需要指定該name的命名空間
# project/urls.py
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path(r"app01/", include("app01.urls", namespace="app01")),
path(r"app02/", include("app02.urls", namespace="app02")),
]
在django2.x中,app01/url.py需要添加 app_name = "app01"
# app01/urls.py
from django.urls import path
from app01 import views
app_name = "app01"
urlpatterns = [
path("login/", views.login, name="Log"),
]
# app01/views.py(需要時設(shè)置)
from django.shortcuts import render
from django.urls import reverse
# Create your views here.
def login(request):
a = reverse("app01:Log")
print("app01:", a)
return render(request, "app01/login.html")
# app01/templates/app01/login.html(需要時設(shè)置)
<form action="{% url "app01:Log" %}" method="post">
<p>用戶名 <input type="text" name="name"></p>
<p>密碼 <input type="password" name="pwd"></p>
<p><input type="submit" value="登錄"></p>
</form>
轉(zhuǎn)換器
對于django2.0版本以后,出現(xiàn)新的path() urlconf,他有兩個好處:
1:url修改維護(hù)變得簡單
2:url獲得的參數(shù)的數(shù)據(jù)類型不再是單純的str,在path中可以通過轉(zhuǎn)換器實現(xiàn)參數(shù)數(shù)據(jù)類型的轉(zhuǎn)換
path()中存在5個內(nèi)置轉(zhuǎn)化器:
=1. str,匹配除了路徑分隔符(/)之外的非空字符串,這是默認(rèn)的形式
2. int,匹配正整數(shù),包含0。
3. slug,匹配字母、數(shù)字以及橫杠、下劃線組成的字符串。
4. uuid,匹配格式化的uuid,如 075194d3-6885-417e-a8a8-6c931e272f00。
5. path,匹配任何非空字符串,包含了路徑分隔符
使用方法:
step1: app01/urls.py
from django.urls import path, re_path
from app01 import views
app_name = "app01"
urlpatterns = [
path("login/", views.login, name="Log"),
# re_path(r"articles/(?P<year>[0-9]{4})/", views.articles),
path("articles/<int:year>/<int:month>/", views.articles),
]
step2: app01/views.py
def articles(request, year, month):
print(year, month)
print(type(year))
return HttpResponse("ok")
- <int: year> 相當(dāng)于re_path()中的 (?P[0-9]{4})
- <> 表示有名分組,year是組名;int表示傳參的數(shù)據(jù)類型是正整數(shù)(但包括0)
自定義轉(zhuǎn)換器
step1: app01/my_converters.py
class YearConverter:
regex = '[0-9]{4}'
def to_python(self, value):
return int(value)
def to_url(self, value):
return '%04d' % value
step2: app01/urls.py
from django.urls import path, re_path, register_converter
from app01 import views, my_converters
register_converter(my_converters.YearConverter, 'year')
app_name = "app01"
urlpatterns = [
path("login/", views.login, name="Log"),
# re_path(r"articles/(?P<year>[0-9]{4})/", views.articles),
# path("articles/<int:year>/<int:month>/", views.articles),
path("books/<year:y>", views.books),
]
step3: app01/views.py
def books(request, y):
print(y, type(y))
return HttpResponse("ok,books")
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Python實現(xiàn)一個轉(zhuǎn)存純真IP數(shù)據(jù)庫的腳本分享
工作中我們常需要使用純真IP數(shù)據(jù)庫內(nèi)的數(shù)據(jù)做分析,下面這篇文章主要給大家介紹了利用Python如何實現(xiàn)一個轉(zhuǎn)存純真IP數(shù)據(jù)庫的相關(guān)資料,對大家具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起看看吧。2017-05-05
Python統(tǒng)計列表中每個元素出現(xiàn)次數(shù)的4種實現(xiàn)
本文主要介紹了Python統(tǒng)計列表中每個元素出現(xiàn)次數(shù)的4種實現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-07-07
python selenium登錄豆瓣網(wǎng)過程解析
這篇文章主要介紹了python selenium登錄豆瓣網(wǎng)過程解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2019-08-08

