Django零碎知识点

无名分组

  

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/([0-9]{4})/$', views.year_archive),
    re_path(r'^articles/([0-9]{4})/([0-9]{2})/$', views.month_archive),
    re_path(r'^articles/([0-9]{4})/([0-9]{2})/([0-9]+)/$', views.article_detail),
]

注意: 

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

  不需要添加前导反斜杠,每一个URL都有.

  r表示引号里边的内容不需要转义.

  捕获的值作为位置参数

 

有名分组

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

  在python中命名正则表达式组的语法是(?P<name>patter), name是组的名字,patter是要匹配的正则式子  

注意: 

  有名分组捕获的值作为关键字参数而不是位置参数,所以需要严格参数的排序

 

 

反向解析

  

posted @ 2019-01-08 20:24  清风吹杨柳  阅读(124)  评论(0编辑  收藏  举报