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), ... ]