Django的路由层

URL配置(URLconf)就像Django所支撑网站的目录.它的本质是URL与要为该URL调用的视图函数之间的映射表,就是以这种方式告诉DJango,对于客户端发来的某个URL调用哪一段逻辑代码对应执行.

简单的路由配置

from django.urls import path, re_path
from app1 import views

urlpatterns = [
    re_path("^articles/2003/$", views.special_case_2003),
    re_path("^articles/([0-9]{4})/$", views.year_archive),
    re_path("^articles/([0-9]{4}/[0-9]{2})/$", views.month_archive),
    re_path("^articles/([0-9]{4})/([0-9]{2})/([0-9]+)/$", views.article_detail)

注意:

  若要从URL中捕获一个值,只需要在它周围放置一对圆括号

  不需要添加一个前导的反斜杠,因为每个URL都有,例如,应该是^articles而不是^/articles.

示例:

'''
 一些请求的例子:

/articles/2005/03/ 请求将匹配列表中的第三个模式。Django 将调用函数views.month_archive(request, '2005', '03')。
/articles/2005/3/ 不匹配任何URL 模式,因为列表中的第三个模式要求月份应该是两个数字。
/articles/2003/ 将匹配列表中的第一个模式不是第二个,因为模式按顺序匹配,第一个会首先测试是否匹配。请像这样自由插入一些特殊的情况来探测匹配的次序。
/articles/2003 不匹配任何一个模式,因为每个模式要求URL 以一个反斜线结尾。
/articles/2003/03/03/ 将匹配最后一个模式。Django 将调用函数views.article_detail(request, '2003', '03', '03')。
   
    '''

 

有名分组

上面的示例使用简单的,没有命名的正则表达式组(通过圆括号)来捕获URL中的值并以位置参数传递给视图,在更高级的用法中,可以使用命名的正则表达式组来捕获URL中的值并以关键字参数传递给视图.

在python正则表达式中,命名正则表达式的语法是(?P<name>pattern), 其中name是组的名称,pattern是要匹配的模式.

下面是以上URLconf使用命名组的重写

from django.urls import path,re_path

from app01 import views

urlpatterns = [
    re_path(r'^articles/2003/$', views.special_case_2003),
    re_path(r'^articles/(?P<year>[0-9]{4})/$', views.year_archive),
    re_path(r'^articles/(?P<year>[0-9]{4})/(?P<month>[0-9]{2})/$', views.month_archive),
    re_path(r'^articles/(?P<year>[0-9]{4})/(?P<month>[0-9]{2})/(?P<day>[0-9]{2})/$', views.article_detail),
]

这个实现与前面的示例完全相同,只有一个差别:捕获的值作为关键字参数而不是位置参数传递给视图函数.例如:

'''
    /articles/2005/03/ 请求将调用views.month_archive(request, year='2005', month='03')函数,而不是views.month_archive(request, '2005', '03')。
    /articles/2003/03/03/ 请求将调用函数views.article_detail(request, year='2003', month='03', day='03')。

    '''

 

分发

'''
At any point, your urlpatterns can “include” other URLconf modules. This
essentially “roots” a set of URLs below other ones.

'''

from django.urls import path,re_path,include
from app01 import views

urlpatterns = [
   re_path(r'^admin/', admin.site.urls),
   re_path(r'^blog/', include('blog.urls')),
]

 

反向解析

在使用Django项目时,一个常见的需求是过得URL的最终形式,以用于嵌入到生成的内容中(视图中和显示给用户的URL等)或者用于处理服务器端的导航(重定向等).

在需要URL的地方,对于不同层级,Django提供不同的工具用于URL反查

  在模板中:使用url模板标签

  在python代码中:使用form django.urls import reverse()函数

urls.py

from django.conf.urls import url

from . import views

urlpatterns = [
    #...
    re_path(r'^articles/([0-9]{4})/$', views.year_archive, name='news-year-archive'),
    #...
]

在模板中:

<a href="{% url 'news-year-archive' 2012 %}">2012 Archive</a>

<ul>
{% for yearvar in year_list %}
<li><a href="{% url 'news-year-archive' yearvar %}">{{ yearvar }} Archive</a></li>
{% endfor %}
</ul>

在python中:

from django.urls import reverse
from django.http import HttpResponseRedirect

def redirect_to_year(request):
    # ...
    year = 2006
    # ...
    return HttpResponseRedirect(reverse('news-year-archive', args=(year,)))   # 同redirect("/path/")

当命名URL时,使用的名称不能与其他名称冲突.

 

posted @ 2018-11-17 16:54  小鸽鸽OvO  阅读(147)  评论(0编辑  收藏  举报