Django的路由层 路由控制之简单配置

Django的路由层

URL配置(URLconf)就像Django 所支撑网站的目录。它的本质是URL与要为该URL调用的视图函数之间的映射表;你就是以这种方式告诉Django,

对于客户端发来的某个URL调用哪一段逻辑代码对应执行

django 路由 1.x 使用过 re_path

django2.x 使用path

先学习使用re_path语法

在django2.x 默认使用path,使用re_path需要重新导入 才能使用

简单的路由配置

 request 是请求对象  所有请求信息都在request里面

View.py 视图函数 导入 HttpResponse 响应对象 ,HttpResponse 返回是字符串

第一个
views.py
from django.shortcuts import render, HttpResponse

# Create your views here.


def special_case_2003(request):
    
    return HttpResponse("special_case_2003")

urls.py 

special_case_2003(request)
from django.contrib import admin
from django.urls import path, re_path

from app01 import views

urlpatterns = [
    path('admin/', admin.site.urls),

    # 路由配置 ---> 视图函数
    re_path(r'^articles/2003/$', views.special_case_2003),  # special_case_2003(request)
    # 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),



]

访问articles/2003/

^articles/2003/$ 是用正则匹配 以什么开头 以什么结尾 以2003结尾  添加^和$ 确保唯一匹配
^articles/2003/  以任何结尾都能匹配上 ,以articles开头
articles/2003/$  以任意开头都可以匹配上 2003结尾
 

第二个

^articles/([0-9]{4})/$  [0-9]任意一个数字 {4} 意思是重复4次  只要是4位数数字,就匹配成功 就做分组
例如
^articles/2004/$
^articles/2005/$
在正则里 加上() 代表分组 ,分组匹配内容作为位置参数传递给对应的视图函数,分组 必须接受新的参数,视图函数要多接受参数

路径匹配和视图函数 是多对一关系
views.py
from django.shortcuts import render, HttpResponse

# Create your views here.


def special_case_2003(request):
    
    return HttpResponse("special_case_2003")


def year_archive(request, year):

    return HttpResponse(year)
urls.py
year_archive(request, 2009)
from django.contrib import admin
from django.urls import path, re_path

from app01 import views

urlpatterns = [
    path('admin/', admin.site.urls),

    # 路由配置 ---> 视图函数
    # re_path(r'^articles/2003/$', views.special_case_2003),  # special_case_2003(request)
    re_path(r'^articles/([0-9]{4})/$', views.year_archive),  # year_archive(request, 2009)
    # 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),


]

 第三个

^articles/([0-9]{4})/([0-9]{2})/$   两个组, [0-9]{4} 代表4位数 数字, [0-9]{2} 代表2位数 数字
 要接受3个参数 

month_archive(request, 2009, 12)

views.py
from django.shortcuts import render, HttpResponse

# Create your views here.


def special_case_2003(request):
    
    return HttpResponse("special_case_2003")


def year_archive(request, year):

    return HttpResponse(year)


def month_archive(request, year, month):

    return HttpResponse(year+"-"+month)

urls.py

from django.contrib import admin
from django.urls import path, re_path

from app01 import views

urlpatterns = [
    path('admin/', admin.site.urls),

    # 路由配置 ---> 视图函数
    # re_path(r'^articles/2003/$', views.special_case_2003),  # special_case_2003(request)
    # re_path(r'^articles/([0-9]{4})/$', views.year_archive),  # year_archive(request, 2009)
    re_path(r'^articles/([0-9]{4})/([0-9]{2})/$', views.month_archive),  # month_archive(request, 2009, 12)
    # re_path(r'^articles/([0-9]{4})/([0-9]{2})/([0-9]+)/$', views.article_detail),


]


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 都有。例如,应该是^articles 而不是 ^/articles
  • 每个正则表达式前面的'r' 是可选的但是建议加上。它告诉Python 这个字符串是“原始的” —— 字符串中任何字符都不应该转义
示例:
    '''
 一些请求的例子:

/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')。
   
    '''


 
原文地址:https://www.cnblogs.com/mingerlcm/p/14164491.html