Django版本差异

Posted on   呱呱呱呱叽里呱啦  阅读(50)  评论(0编辑  收藏  举报

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())

非详尽

(评论功能已被禁用)
相关博文:
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现
点击右上角即可分享
微信分享提示