您好,登錄后才能下訂單哦!
分組
分組的目的:讓服務端獲得url中的具體數(shù)據,通過分組,把需要的數(shù)據按函數(shù)傳參的方式傳遞給服務器后臺
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-有名分組
在更高級的用法中,可以使用命名的正則表達式組來捕獲URL 中的值并以關鍵字 參數(shù)傳遞給視圖。
在Python 正則表達式中,命名正則表達式組的語法是(?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), ]
捕獲的值作為關鍵字參數(shù)而不是位置參數(shù)傳遞給視圖函數(shù)
# app01/views.py def articles(request, month, year ): print(year, month) return HttpResponse(year+"-"+month)
分發(fā)
分發(fā)的目的:解決一個django項目中因為存在多個應用app導致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> <h3>app01_login</h3> <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,造成的維護成本巨大
此時可以給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")
命名空間
命名空間要配合反向解析使用,當存在多個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(需要時設置) 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(需要時設置) <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>
轉換器
對于django2.0版本以后,出現(xiàn)新的path() urlconf,他有兩個好處:
1:url修改維護變得簡單
2:url獲得的參數(shù)的數(shù)據類型不再是單純的str,在path中可以通過轉換器實現(xiàn)參數(shù)數(shù)據類型的轉換
path()中存在5個內置轉化器:
=1. str,匹配除了路徑分隔符(/)之外的非空字符串,這是默認的形式
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")
自定義轉換器
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")
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持億速云。
免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。