基本格式
from django.conf.urls import url
urlpatterns = [
url(正規表示式, views檢視,引數,別名),
]
django 2.0版本的路由系統
from django.urls import path,re_path
urlpatterns = [
path('articles/2003/', views.special_case_2003),
]
基本配置
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^articles/2003/$', views.special_case_2003),
]
注意事項
補充說明
分組:使用簡單的正規表示式分組匹配(通過圓括號)來捕獲url中的值並以位置引數形式傳遞給檢視函式
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^articles/([0-9])/$', views.year_archive),
url(r'^articles/([0-9])/([0-9])/$', views.month_archive),
]
命名分組:使用分組命名匹配的正規表示式組來捕獲url中的值並以關鍵字引數形式傳遞給檢視函式
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^articles/(?p[0-9])/$', views.year_archive),
url(r'^articles/(?p[0-9])/(?p[0-9])/$', views.month_archive),
]
uplconf匹配的位置:
urlconf 不檢查請求的方法,即所有的請求方法(同乙個url的post、get、head等),都將路由到相同的函式
捕獲的引數都是字串:每個在urlconf中捕獲的引數都作為乙個普通的python字串傳遞給檢視函式,無論正規表示式使用的是什麼匹配方式
有個別情況,需要檢視函式指定預設值
# urls.py中
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^blog/$', views.page),
url(r'^blog/page(?p[0-9]+)/$', views.page),
]# 兩個url模式指向相同的view,但是第乙個模式並沒有從url中捕獲任何東西
# views.py中,可以為num指定預設值
def page(request, num="1"):
pass
# 如果第乙個模式匹配上了,page函式將使用其預設引數num=「1」,如果第二個模式匹配,page將使用正規表示式捕獲到的num值
include:路由分發
from django.conf.urls import include, url
urlpatterns = [
url(r'^admin/', admin.site.urls),
]
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^blog/$', views.blog),
url(r'^blog/(?p[0-9])/(?p\d)/$', views.blogs),
]
from django.conf.urls import include, url
urlpatterns = [
]
4.1 url命名
url的第四個引數是起乙個別名,即乙個可選的name引數
命名方式:name = '別名'
from django.conf.urls import url
urlpatterns = [
url(r'^home', views.home, name='home'), # 給我的url匹配模式起名為 home
url(r'^index/(\d*)', views.index, name='index'), # 給我的url匹配模式起名為index
]
4.2 反向解析
反向解析:通過別名獲取完整url路徑
情況一:靜態路由
from django.conf.urls import url
urlpatterns = [
url(r'^blog/$', views.blog, name='blog'),
]
from django.urls import reverse
reverse('blog') # 完整url路徑:/blog/
情況二:分組
from django.conf.urls import url
urlpatterns = [
url(r'^blog/([0-9])/(\d)/$', views.blogs, name='blogs'),
]
from django.urls import reverse
reverse('blogs',args=('2019','06')) # 完整url路徑:/blog/2019/06/
情況三:命名分組
from django.conf.urls import url
urlpatterns = [
url(r'^blog/(?p[0-9])/(?p\d)$', views.blogs, name='blogs'),
]
from django.urls import reverse
reverse('blogs',kwargs=) # 完整url路徑:/blog/2019/06/
專案目錄下的urls.py
from django.conf.urls import url, include
urlpatterns = [
]
from django.conf.urls import url
urlpatterns = [
url(r'^(?p\d+)/$', views.detail, name='detail')
]
from django.conf.urls import url
urlpatterns = [
url(r'^(?p\d+)/$', views.detail, name='detail')
]
反向解析語法:'命名空間名稱:url名稱'
from django.urls import reverse
注意:如果有多層路由分發,有了多個命名空間名稱,都要把命名空間名稱一層一層加在別名前面
Django之(URL)路由系統
簡而言之,django的路由系統作用就是使views裡面處理資料的函式與請求的url建立對映關係。使請求到來之後,根據urls.py裡的關係條目,去查詢到與請求對應的處理方法,從而返回給客戶端http頁面資料 django 專案中的url規則定義放在project 的urls.py目錄下,預設如下 ...
Django的url路由系統簡介
from django.conf.urls import url from django.contrib import admin urlpatterns url r admin admin.site.urls from django.conf.urls import url from django...
Django基礎二之URL路由系統
基本格式 from django.conf.urls import url 迴圈urlpatterns,找到對應的函式執行,匹配上乙個路徑就找到對應的函式執行,就不再往下迴圈了,並給函式傳乙個引數request,就是請求資訊的所有內容 urlpatterns url 正規表示式,views檢視函式,...