配置路由urlconf
一般配置路由在urls.py文件中
urlpatterns = [path('正則表達式',views.視圖函數,參數,別名), ] 參數說明: 1、一個正則表達式字符串 2、一個可調用對象,通常為一個視圖函數或一個指定視圖函數路徑的字符串 3、可選的要傳遞給視圖函數的默認參數(字典形式) 4、一個可選的name參數(別名) 例子: urlpatterns = [path('index/<int:year>',views.hello),path('addstudent/<str:name>',views.add_student), ]- 注意
path轉換器
-
django默認情況下內置的路由轉換器:
1 str 匹配任何非空的字符串,不包含/ 為django的默認轉換器 path(‘index’,add/str:name,views.test)
2 int 匹配0和正整數,返回一個int類型 path(‘index’,add/int:name,views.test)
3 slug 匹配任何ascii字符以及連接符 下劃線 例如:lixinyu_1-test (不常用)
4 uuid 存儲通用唯一識別碼 但是字母必須小寫,必須使用破折號 返回uuid對象 (不常用)
5 path 匹配任何非空字符串,可以包含/ 此轉換器可以匹配整個url而不是一段一段url字符串(不常用)
正則表達式路由
- 正則表達式中 使用的是re_path()
-
在python正則表達式中,命名正則表達式的語法是(?Ppattern),其中name是組的名稱,pattern是需要匹配的規則
-
特點 1 year中匹配不到非四位數字,這是正則表達式決定的
2 傳遞給視圖的所有參數都是字符串類型例
from django.urls improt path,re_path
from 項目app文件夾 import views
urlpatterns = [
path(‘add/2003/’,views.add_student),
#表示add/2003/這個路徑映射views模塊的add_student函數
re_path(r’^add(?P[0-9]{4})/KaTeX parse error: Expected 'EOF', got '#' at position 28: …_student), #?表示匹配4個0-9的任意數字,…’, views.add_student),
表示匹配數字,不進行傳參。
-
指定視圖參數的默認值
-
使用視圖參數的默認值就是給視圖函數中賦值,而后傳遞到路由中
例
from django.urls import path
from blog import views
urlpatterns = [
path(‘admin/’, admin.site.urls),
path(‘blog/int:num/’,views.study),
path(‘blog/pageint:num/’,views.study),
]#views.py中創建
def study(request, num=1):
return HttpResponse(‘展示內容’)地址內輸入
http://127.0.0.1:8000/blog/1/
http://127.0.0.1:8000/blog/page1/兩個URL模式指向同一個視圖views.study —— 但是第一個模式不會從URL 中捕獲任何值。如果第一個模式匹配,page() 函數將使用num參數的默認值"1"。如果第二個模式匹配,page() 將使用正則表達式捕獲的num 值。
錯誤頁面處理
當Django找不到與請求匹配的URL時,或者拋出一個異常時,將調用一個錯誤處理視圖。錯誤視圖包括400、403、404和500,分別表示請求錯誤、拒絕服務、頁面不存在和服務器錯誤-
handler400 ——django.conf.urls.handler400 請求錯誤
-
handler403 ——django.conf.urls.handler403 拒絕服務
-
handler404 ——django.conf.urls.handler404 頁面不存在
-
handler500 ——django.conf.urls.handler500 服務器錯誤
這些值可以在根URLconf中設置 在其它app中的二級URLconf中設置這些變量無效
Django中又內置的HTML模板,用域返回錯誤頁面給用戶,也可以自定義錯誤頁面
首先在根URLconf中額外增加下面的條目
from django.conf.urls import url
from blog import views
urlpatterns = [
url(r’^blog/ ′ , v i e w s . s t u d y ) , u r l ( r ′ b l o g / p a g e ( ? P < n u m > [ 0 ? 9 ] + ) / ', views.study), url(r'^blog/page(?P<num>[0-9]+)/ ′,views.study),url(r′blog/page(?P<num>[0?9]+)/’, views.study),
]
#增加的條目
handler400 = views.bad_request
handler403 = views.permission_denied
handler404 = views.page_not_found
handler500 = views.page_error在views.py中增加四個處理視圖
def page_not_found(request):
return render(request, ‘404.html’)def page_error(request):
return render(request, ‘500.html’)def permission_denied(request):
return render(request, ‘403.html’)def bad_request(request):
return render(request, ‘400.html’)
而后根據需要在templates文件夾下創建404.html、403.html、400.html、500.html四個頁面
urls分層模塊化(路由分發)
-
通常在每個app里各自創建一個urls.py路由模塊,從根路由出發,將所屬url請求全部轉發到想用的urls.py模塊中
from django.urls import include,path
urlpatterns = [
path(‘community/’, include(‘aggregator.urls’)),
path(‘contact/’, include(‘contact.urls’))
]
路由轉發使用的時include()方法,需要提前導入,參數是轉發目的地路徑的字符串,路徑以圓點分割
例子中的正則表達式沒有包含$,但是包含一個末尾的斜杠,每當Django遇到include()(來自django.conf.urls.include())時,會去掉url中匹配的部分并將剩下的字符串發送給include的URLconf做進一步處理,也就是轉發二級路由。
個人觀點:
路由分發就是項目本身的路由不承擔過多的路由,將路由分發到子app的urls.py中,功能類似于藍圖的作用
在項目本身urls.py里導入include,本身主urls.py只負責設置子路由的路徑,而后在子app設置路由參數。
主app url_pro/urls.pyfrom django.contrib import admin from django.urls import path,re_path,include from url_app import views urlpatterns = [path('admin/', admin.site.urls),path('abc/',include('url_app.urls',namespace='url_app')),path('url_app1/', include('url_app1.urls',namespace='url_app1')) ]子app url_app1/urls.pyfrom django.urls import path,re_path from . import views# 子路由文件 app_name = 'url_app1' urlpatterns = [#path('',views.index)#re_path(r'^$',views.index)#re_path(r'([a-z]{3})/(\d+)/',views.index)#path('<int:id_>/<str:name>/',views.index)re_path(r'^$',views.index,name='index') ]url_app/urls.pyfrom django.urls import path,re_path from . import views # 子路由文件app_name = 'url_app' urlpatterns = [#path('',views.index)#re_path(r'^$',views.index)#re_path(r'([a-z]{3})/(\d+)/',views.index)#path('<int:id_>/<str:name>/',views.index)path('test/',views.test,name='test'),path('redir/',views.redir,name='redir'),re_path(r'index/',views.index,name='index') ]問:URLconf匹配請求URL中的哪些部分?
答 : 請求的URL被看做是一個普通的Python字符串,URLconf在其上查找并匹配。進行匹配時將不包括GET或POST請求方式的參數以及域名。 URLconf不檢查使用何種HTTP請求方法,所有請求方法POST、GET、HEAD等都將路由到同一個URL的同一個視圖。在視圖中,才根據具體請求方法的不同,進行不同的處理。 例如: 在http://qzone.qq.com/8436830/的請求中,URLconf將查找8436830 http://qzone.qq.com/8436830/?page=4的請求中,URLconf也將查找8436830總結
以上是生活随笔為你收集整理的配置路由urlconf的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 纯CSS3模拟iPhone X背景切换动
- 下一篇: Spring Boot入门(08):整合