IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> Python知识库 -> django基础知识(3) -> 正文阅读

[Python知识库]django基础知识(3)

目录

路由配置系统URLconf

一般写法

使用正则表达式

错误页面处理

自定义错误页面

urls分层模块化(路由分发)

? ? ? ? include()方法

参数嵌套

反向解析

命名空间


路由配置系统URLconf

一般写法

"""
urlpatterns = [
    path(正则表达式, views视图函数,参数,别名),
]
参数说明:
1、一个正则表达式字符串
2、一个可调用对象,通常为一个视图函数或一个指定视图函数路径的字符串
3、可选的要传递给视图函数的默认参数(字典形式)
4、一个可选的name参数(别名)
"""

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:slug>/', views.article_detail),
    ]


"""
1、尖括号<>,捕获一段url中的值, <int:year>,将捕获的值转换成int类型,默认情况为sr
2、规则的前面不需要添加/
"""


"""
例子:

/articles/2005/03/  对应第三条
/articles/2003/  对应第一条
/articles/2003  未匹配,后面缺少/

"""
  • <>匹配
    • str:默认值,匹配任何非空字符串,不含/
    • int:匹配0和正整数,返回一个int类型
    • slug:可理解为注释、后缀、附属等概念,匹配任何ASCII字符以及连接符和下划线
    • uuid:匹配一个uuid格式的对象,返回一个UUID对象
    • path:匹配任何非空字符串,包含路径分隔符’/‘
  • 自定义<>匹配
    • """
      1、类属性regex:一个字符串形式的正则表达式属性;
      2、to_python(self, value) 方法: 将匹配到的字符串转换为你想要的那个数据类型,并传递给视图函数
      3、to_url(self, value)方法: 将Python数据类型转换为一段url
      """
      class FourDigitYearConverter:
          regex = '[0-9]{4}'
          def to_python(self, value):
              return int(value)
          def to_url(self, value):
              return '%04d' % value
    • ?在URLconf中注册自定义转换器类
    • from django.urls import path, register_converter
      from . import converters, 
      viewsregister_converter(converters.FourDigitYearConverter, 'yyyy')
      urlpatterns = [
          path('articles/2003/', views.special_case_2003),
          path('articles/<yyyy:year>/', views.year_archive),
          ...
          ]

使用正则表达式

????????命名正则表达式组的语法是: (?P<name>pattern),其中name是组的名称,pattern是需要匹配的规则

from django.urls import path, re_path
from . import views
urlpatterns = [
    path('articles/2003/', views.special_case_2003),
    #表示articles/2003/这个路径映射views模块的special_case_2003函数
    re_path(r'^articles/(?P<year>[0-9]{4})/$', views.year_archive),
    #表示匹配4个0-9的任意数字
    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<slug>[\w-]+)/$', views.article_detail),
    ]
#注意:上面匹配都加了小括号,这些括号里面的值会当作参数传递到后面的视图函数中

re_path与path()不同的主要在于两点:

  1. year中匹配不到10000等非四位数字,这是正则表达式决定的
  2. 传递给视图的所有参数都是字符串类型。而不像path()方法中可以指定转换成某种类型。

错误页面处理

????????错误视图包括400、403、404和500,分别表示请求错误、拒绝服务、页面不存在和服务器错误:

  • handler400 —— django.conf.urls.handler400。

  • handler403 —— django.conf.urls.handler403。

  • handler404 —— django.conf.urls.handler404。

  • handler500 —— django.conf.urls.handler500。

  • 相当的丑

自定义错误页面

# urls.py
from django.conf.urls import url
from . import views
urlpatterns = [
    url(r'^blog/$', views.page),
    url(r'^blog/page(?P<num>[0-9]+)/$', views.page),
]
# 增加的条目
handler400 = views.bad_request
handler403 = views.permission_denied
handler404 = views.page_not_found
handler500 = views.page_error


# views.py

def page_not_found(request):
    return render(request, '404.html')
def page_error(request):
    return render(request, '500.html')
def permission_denied(request):
    return render(request, '403.html')
def bad_request(request):
    return render(request, '400.html')

urls分层模块化(路由分发)

? ? ? ? include()方法

????????通常,我们会在每个app里,各自创建一个urls.py路由模块,然后从根路由出发,将app所属的url请求,全部转发到相应的urls.py模块中

from django.urls import include, path
urlpatterns = [
    # ... snip ...
    path('community/', include('aggregator.urls'),{'test':0}),
    path('contact/', include('contact.urls')),
    # ... snip ...
]

????????include()方法,参数是转发目的地路径的字符串,路径以圆点分割。

? ? ? ? 例子中正则表达式没有包含$(字符串结束匹配符),但是包含一个末尾的斜杠。

????????每当Django 遇到include()(来自django.conf.urls.include())时,它会去掉URL中匹配的部分并将剩下的字符串发送给include的URLconf做进一步处理

? ? ? ? 另外一种方式:include也可以包含一个url()实例列表

from django.urls import include, path
from apps.main import views as main_views
from credit import views as credit_views
extra_patterns = [
    path('reports/', credit_views.report),
    path('reports/<int:id>/', credit_views.report),
    path('charge/', credit_views.charge),
]
urlpatterns = [
    path('', main_views.homepage),
    path('help/', include('apps.help.urls')),
    path('credit/', include(extra_patterns)),
]

? ? ? ? 好处是可以将具有相同开头给url分开写:

from django.urls import path
from . import views
urlpatterns = [
    path('<page_slug>-<page_id>/history/', views.history),
    path('<page_slug>-<page_id>/edit/', views.edit),
    path('<page_slug>-<page_id>/discuss/', views.discuss),
    path('<page_slug>-<page_id>/permissions/', views.permissions),
]


"""
变成
"""
from django.urls import include, path
from . import views
urlpatterns = [
    path('<page_slug>-<page_id>/', include([
        path('history/', views.history),
        path('edit/', views.edit),
        path('discuss/', views.discuss),
        path('permissions/', views.permissions),
    ])),
]

????????被转发的URLconf会收到来自父URLconf捕获的所有参数

参数嵌套

????????正则表达式允许嵌套参数,Django将解析它们并传递给视图

from django.urls import re_path
urlpatterns = [
    re_path(r'^blog/(page-(\d+)/)?$', blog_articles),   # bad
    re_path(r'^comments/(?:page-(?P<page_number>\d+)/)?$', comments),  # good]

????????两个模式都使用嵌套的参数,其解析方式是:blog/page-2/

  • 第一个将匹配page-2,并带有两个位置参数:page-2,2
  • 第二个将匹配page_number,并带有一个关键字参数{"page_number":2},外围?:不捕获

反向解析

? ? ? ? 使用name

????????随着功能的增加,路由层的 url 发生变化,就需要去更改对应的视图层和模板层的 url,非常麻烦,不便维护。

????????这时我们可以利用反向解析,当路由层 url 发生改变,在视图层和模板层动态反向解析出更改后的 url,免去修改的操作。

????????反向解析一般用在模板中的超链接及视图中的重定向

from django.urls import path
from . import views
urlpatterns = [
    #...
    path('articles/<int:year>/', views.year_archive, name='news-year-archive'),
    #...
]


from django.http import HttpResponseRedirect
from django.urls import reverse
def redirect_to_year(request):
    # ...
    year = 2006
    # ...
    return HttpResponseRedirect(reverse('news-year-archive', args=(year,)))


<a href="{% url 'news-year-archive' 2012 %}">2012 Archive</a>
{# Or with the year in a template context variable: #}
<ul>
{% for yearvar in year_list %}
<li><a href="{% url 'news-year-archive' yearvar %}">{{ yearvar }} Archive</a></li>
{% endfor %}
</ul>

命名空间

????????路由别名 name 没有作用域,Django 在反向解析 URL 时,会在项目全局顺序搜索,当查找到第一个路由别名 name 指定 URL 时,立即返回。当在不同的 app 目录下的urls 中定义相同的路由别名 name 时,可能会导致 URL 反向解析错误。

1、第一种,设置app_name属性

from django.urls import path
from . import views
app_name = 'polls'
urlpatterns = [
    path('', views.IndexView.as_view(), name='index'),
    path('<int:pk>/', views.DetailView.as_view(), name='detail'),
    ...
]


urls.py
from django.urls import include, path
urlpatterns = [
    path('polls/', include('polls.urls')),
]

2、第二种,include((<list?of?path()/re_path()?instances>,?<application?namespace>))

rom django.urls import include, path
from . import views
polls_patterns = ([
    path('', views.IndexView.as_view(), name='index'),
    path('<int:pk>/', views.DetailView.as_view(), name='detail'),
], 'polls')
urlpatterns = [
    path('polls/', include(polls_patterns)),
]

  Python知识库 最新文章
Python中String模块
【Python】 14-CVS文件操作
python的panda库读写文件
使用Nordic的nrf52840实现蓝牙DFU过程
【Python学习记录】numpy数组用法整理
Python学习笔记
python字符串和列表
python如何从txt文件中解析出有效的数据
Python编程从入门到实践自学/3.1-3.2
python变量
上一篇文章      下一篇文章      查看所有文章
加:2021-07-10 14:30:21  更:2021-07-10 14:31:43 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/3 18:33:50-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码