URL配置(URLconf)就像Django所支撐網站的目錄。它的本質是URL與要爲該URL調用的視圖函數之間的映射表;就是以這種方式告訴Django,對於客戶端發來的某個URL,調用哪一段邏輯代碼對應執行。python
from django.conf.urls import url from app01 import views urlpatterns = [ url(r"^articles/2003/$", views.func) # func(request) url(r"^articles/(\d{4})/(\d{2})/$", views.func) # func(request, "2013", "07") ]
from django.urls import path, re_path from app01 import views urlpatterns = [ path(r"^articles/2003/$", views.func) # func(request) re_path(r"^articles/(\d{4})/(\d{2})/$", views.func) # func(request, "2013", "07") ]
注:git
上面的示例使用簡單的、沒有命名的正則表達式組(經過圓括號)來捕獲URL 中的值並以位置參數傳遞給視圖。在更高級的用法中,可使用命名的正則表達式組來捕獲URL 中的值並以關鍵字參數傳遞給視圖。正則表達式
在Python 正則表達式中,命名正則表達式組的語法是(?P<name>pattern)
,其中name
是組的名稱,pattern
是要匹配的模式。django
from django.conf.urls import url from app01 import views urlpatterns = [ url(r"^articles/(?P<y>\d{4})/(?P<d>\d{2})/$", views.func) # func(request, y="2013", d="07") ]
from django.urls import path, re_path from app01 import views urlpatterns = [ re_path(r"^articles/(?P<y>\d{4})/(?P<d>\d{2})/$", views.func) # func(request, y="2013", d="07") ]
在實際應用中,這意味你的URLconf會更加清晰且不容易產生參數順序問題的錯誤——你能夠在你的視圖函數定義中從新安排參數的順序。固然,這些好處是以簡潔爲代價服務器
''' At any point, your urlpatterns can 「include」 other URLconf modules. This essentially 「roots」 a set of URLs below other ones. '''
# 全局urls.py: from django.urls import path,re_path,include from app01 import views urlpatterns = [ re_path(r'^admin/', admin.site.urls), re_path(r'^app01/', include('app01.urls')), ] # app01 urls.py: from django.urls import path, re_path from app01 import views urlpatterns = [ path("articles/", views.func) # http://127.0.0.1:8000/app01/articles/ ]
在使用Django項目時,一個常見的需求是得到URL的最終形式,以用於嵌入到生成的內容中(視圖中和顯示給用戶的URL等)或者用於處理服務器端的導航(重定向等)app
人們強烈但願不要硬編碼這些URL(費力、不可擴展且容易產生錯誤)或者設計一種與URLconf絕不相關的專門的URL生成機制,由於這樣容易致使必定程度上產生過時的URL函數
在須要URL的地方,對於不一樣層級,Django提供不一樣的工具用於URL反查:工具
from django.urls import reverse
urls.py:網站
from django.urls import path,re_path from app01 import views urlpatterns = [ #... re_path(r'^articles/(\d{4})/$', views.year_archive, name='news-year-archive'), #... ]
在模板中:ui
<a href="{% url 'news-year-archive' 2012 %}">2012 Archive</a>
在Python中:
from django.shortcuts import render, redirect, reverse def redirect_to_year(request): #... year = "2006" #... return redirect(reverse("news-year-archive", args=(year,)))
當命名你的URL模式時,請確保使用的名稱不會與其它應用中名稱衝突。若是你的URL模式叫作comment,而另一個應用中也有一個一樣的名稱,當你在模板中使用這個名稱的時候不能保證將插入哪一個URL,在URL名稱中加上一個前綴,好比應用的名稱,將減小衝突的可能。建議使用myapp-comment而不是comment
因爲name沒有做用域,Django在反解URL時,會在項目上全局順序搜索,當查找到第一個name指定URL時,當即返回。
在開發項目時,會常用name屬性反解出URL,當不當心在不一樣的app的urls中定義相同的name時,可能會致使URL反解錯誤,爲了不這種事情發生,引入了命名空間。
命名空間(英語:Namespace)是表示標識符的可見範圍。一個標識符可在多個命名空間中定義,它在不一樣的命名空間中的含義是互不相干的。這樣,在一個新的命名空間中可定義任何標識符,它們不會與任何已有的標識符發生衝突,由於已有的定義都處於其它命名空間中。
project urls.py:
urlpatterns = [ re_path(r'^admin/', admin.site.urls), re_path(r'^app01/', include("app01.urls",namespace="app01")), re_path(r'^app02/', include("app02.urls",namespace="app02")), ]
app01 urls.py:
urlpatterns = [ re_path(r'^index/', views.index,name="index"), ]
app02 urls.py:
urlpatterns = [ re_path(r'^index/', views.index,name="index"), ]
app01 views:
from django.shortcuts import render, redirect, reverse def redirect_to_index(request): return redirect(reverse("app01:index"))
app02 views:
from django.shortcuts import render, redirect, reverse def redirect_to_index(request): return redirect(reverse("app02:index"))
urlpatterns = [ re_path('articles/(?P<year>[0-9]{4})/', year_archive), re_path('article/(?P<article_id>[a-zA-Z0-9]+)/detail/', detail_view), re_path('articles/(?P<article_id>[a-zA-Z0-9]+)/edit/', edit_view), re_path('articles/(?P<article_id>[a-zA-Z0-9]+)/delete/', delete_view), ]
兩個問題:
在Django 2.0中,可使用path解決以上問題
from django.urls import path from . import views urlpatterns = [ path('articles/2003/', views.special_case_2003), path('articles/<int:year>/', views.year_archive), path('articles/<int:year>/<int:month>/', views.month_archive), path('articles/<int:year>/<int:month>/<slug>/', views.article_detail), ]
基本規則:
如下根據2.0官方文檔而整理的示例分析表:
"""文檔原文Path converters, 暫且翻譯爲轉化器"""
Django默認支持如下5個轉化器:
對於一些複雜或者複用的須要, 能夠定義本身的轉化器。轉化器是一個類或接口,它地要求有三點:
class FourDigitYearConverter: regex = '[0-9]{4}' def to_python(self, value): return int(value) def to_url(self, value): return '%04d' % value
使用register_converter將其註冊到URL配置中:
from django.urls import register_converter, path from app01 import converters, views register_converter(converters.FourDigitYearConverter, 'yyyy') urlpatterns = [ path('articles/2003/', views.special_case_2003), path('articles/<yyyy:year>/', views.year_archive), ... ]