首页 > 其他分享 >Django版本差异

Django版本差异

时间:2022-11-27 21:44:58浏览次数:43  
标签:articles converter views 差异 value Django 版本 import path

Django版本差异

# 一、路由层

'''
django 1.x路由层使用url方法
django 2.x/3.x路由层使用path方法 可以根据习惯使用re_path
path方法支持5种转换器

'''
from django.urls import path,re_path

from app01 import views

urlpatterns = [

    path('articles/<int:year>/', views.year_archive), 
    '''<int:year>相当于一个有名分组,其中int是django提供的转换器,相当于正则表达式,专门用于匹配数字类型,而year则是我们为有名分组命的名,并且int会将匹配成功的结果转换成整型后按照格式(year=整型值)传给函数year_archive
    '''


    path('articles/<int:article_id>/detail/', views.detail_view), 
    path('articles/<int:article_id>/edit/', views.edit_view),
    path('articles/<int:article_id>/delete/', views.delete_view),
]

'''
str,匹配除了路径分隔符(/)之外的非空字符串,这是默认的形式
int,匹配正整数,包含0。
slug,匹配字母、数字以及横杠、下划线组成的字符串。
uuid,匹配格式化的uuid,如 075194d3-6885-417e-a8a8-6c931e272f00。
path,匹配任何非空字符串,包含了路径分隔符(/)(不能用)
'''

path('articles/<int:year>/<int:month>/<slug:other>/', views.article_detail) 
'''针对路径http://127.0.0.1:8000/articles/2009/123/hello/,path会匹配出参数year=2009,month=123,other='hello'传递给函数article_detail'''

# 除了默认的5个转换器,还提供了自定义转换器类 eg:
# 自定义转换器
class MonthConverter:
    regex='\d{2}' # 属性名必须为regex

    def to_python(self, value):
        return int(value)

    def to_url(self, value):
        return value # 匹配的regex是两个数字,返回的结果也必须是两个数字
# 在urls.py注册转换器
from django.urls import path,register_converter
from app01.path_converts import MonthConverter

register_converter(MonthConverter,'mon')

from app01 import views


urlpatterns = [
    path('articles/<int:year>/<mon:month>/<slug:other>/', views.article_detail, name='aaa'),

]
'''
Registering custom path converters¶
For more complex matching requirements, you can define your own path converters.

A converter is a class that includes the following:

A regex class attribute, as a string.

A to_python(self, value) method, which handles converting the matched string into the type that should be passed to the view function. It should raise ValueError if it can’t convert the given value. A ValueError is interpreted as no match and as a consequence a 404 response is sent to the user unless another URL pattern matches.

A to_url(self, value) method, which handles converting the Python type into a string to be used in the URL. It should raise ValueError if it can’t convert the given value. A ValueError is interpreted as no match and as a consequence reverse() will raise NoReverseMatch unless another URL pattern matches.
For example:

class FourDigitYearConverter:
    regex = '[0-9]{4}'

    def to_python(self, value):
        return int(value)

    def to_url(self, value):
        return '%04d' % value
Register custom converter classes in your URLconf using register_converter():


from django.urls import path, register_converter

from . import converters, views

register_converter(converters.FourDigitYearConverter, 'yyyy')

urlpatterns = [
    path('articles/2003/', views.special_case_2003),
    path('articles/<yyyy:year>/', views.year_archive),
    ...
]
'''

# 二、模型层
# 1.x外键默认级联删除、级联更新,但2.x/3.x需要自己手动配置参数
# 1.x
models.ForeignKey(to='Publish')
# 2.x/3.x
models.ForeignKey(to='Publish',on_delete=models.CASCADE(),on_update=models.CASCADE())

非详尽

标签:articles,converter,views,差异,value,Django,版本,import,path
From: https://www.cnblogs.com/missfxy/p/16930666.html

相关文章

  • Django-ORM简介
    DjangoORM目录DjangoORM字段的增删改查数据的增删改查创建表关系null#即对象关系模型#类——表#对象——记录#对象属性(值)——记录某个字段(值)#在models.py中写......
  • 学习笔记-Django框架的使用
    前言:本博客为技术小白的记录学习过程,有错误或不解的地方请指出!!!一.安装和创建项目1.安装1.1命令行下载pip3installdjango==1.11.11 (可以跟镜像地址:-i+镜像地址......
  • strcmp绕过/漏洞 PHP5.3之前版本
    待补充1111111111111111111111111111111111111111111pache中的.htaccess.htaccess是什么?全称是HypertextAccess(超文本入口).htaccess文件也被成为分布式配置文件,提供......
  • 转 若依框架代码生成(前后端分离版本)
    https://www.cnblogs.com/dusucyy/ 若依框架代码生成(前后端分离版本) 1、修改代码生成配置编辑resources目录下的application.yml最下面,代码生成这一块。author:......
  • Visual Studio新版本两项改变
    当C++函数中的return关键字后跟非内置类型的表达式时,执行该return语句会将表达式的结果复制到调用函数的返回槽(ReturnSlot)中。为此,将调用非内置类型的复制或移动构......
  • django 定时任务 Error: no such table: django_apscheduler_djangojob
    使用django的定时任务一般出现各种问题例如找不到这个注册表或者urls不存在等etc....版本不兼容我的版本django2.2.8django_apscheduler==0.2.12APScheduler~......
  • django之ModelForm配合Select2之用法
    django在开发时有很强大的功能,但是有一些功能还是不够的,django给我们强大的自增强功能。Select2https://select2.org开发时可以直接使用CND:<linkhref="https://cdn.j......
  • 最新版gym-0.26.2下Atari环境的安装以及环境版本v0,v4,v5的说明
    强化学习的游戏仿真环境可以分为连续控制和非连续控制两类,其中连续控制的以mujoco为主,而非连续控制的以Atari游戏为主,本文对gym下的Atari环境的游戏环境版本进行一定的介绍......
  • asp.net中的报销多级审批工作流 (状态机版本)
    asp.net中的报销多级审批工作流(状态机版本)     上篇​​asp.net中的报销多级审批工作流​​,提到参考了网上一个具体的项目,项目中用状态机工作流完成,基于学习......
  • 了解快照隔离和行版本控制
    了解快照隔离和行版本控制启用快照隔离之后,每个事务的已更新行版本在tempdb中维护。唯一的事务序列号标识每个事务,并且为每个行版本记录这些唯一的编号。事务使用序列号......