Jquery中文网 www.jquerycn.cn
Jquery中文网 >  Python编程  >  Python 框架  >  正文 django1和2的区别

django1和2的区别

发布时间:2021-01-15   编辑:www.jquerycn.cn
jquery中文网为您提供django1和2的区别等资源,欢迎您收藏本站,我们将为您提供最新的django1和2的区别资源

Django

路由匹配使用path和re_path代替url函数

path匹配绝对路径,re_path匹配正则表达式路径

from django.urls import path, re_path

urlpatterns = [

path(’’, index_views),

path(‘login/’,login_views),

path(‘register/’,register_views),

path(‘check/’,check_views),

path(“test/”,test_views),

re_path(“str:user”,user_views),

re_path(“index/(\d )”,showcontent)

]

Django的path默认支持以下5个转化器:

str,匹配除了路径分隔符(/)之外的非空字符串,这是默认的形式

int,匹配正整数,包含0。

slug,匹配字母、数字以及横杠、下划线组成的字符串。

uuid,匹配格式化的uuid,如 075194d3-6885-417e-a8a8-6c931e272f00。

path,匹配任何非空字符串,包含了路径分隔符

django1与2路由的差别

在django1中的url在django2中为re_path
django2中新增了path
    1.from django.urls import path
    2.不支持正则,精准匹配
    3.有5个转换器(int,str,slug,path,uuid)
    4.自定义转换器:
        1 写一个类:
		class Test:
			regex = '[0-9]{4}'
			def to_python(self, value):
				# 写一堆处理
				value=value 'aaa'
				return value
			def to_url(self, value):
				return 'd' % value
		2 from django.urls import register_converter
		3 register_converter(Test,'ttt')
		4 path('index/<ttt:year>', views.index,name='index'),

MVC和MTV

  M          T                 V	models	   template         views

	M          V                 C(路由 views)	models    模板    控制器	

其实MVC与MTV是一样的,django中为MTV,数据交互层,视图层以及控制层

视图层:request对象

request对象:
# form表单,不写method ,默认是get请求
#     1 什么情况下用get:请求数据,请求页面,
#     2 用post请求:向服务器提交数据
# request.GET  字典
# request.POST  字典
# 请求的类型
# print(request.method)
# 路径
# http://127.0.0.1:8000/index/ppp/dddd/?name=lqz
# 协议:ip地址和端口/路径?参数(数据)  
# print(request.path) -->/index/ppp/dddd/
# print(request.get_full_path()) -->/index/ppp/dddd/?name=lqz

JsonResponse

向前端页面发json格式字符串

封装了jsonfrom django.http import JsonResponse

dic={'name':'lqz','age':18}
li=[1,2,3,4]return JsonResponse(li,safe=False)

您可能感兴趣的文章:
django1和2的区别
css中display:none和visibility:hidden的区别
php global static与$GLOBALS的区别
硬盘分区表丢失怎么办 DiskGenius硬盘分区表丢失找回分区办法
Linux系统分区详解
php7跟5区别
什么是前端和后端
mysql 中 delete from 表名和truncate table 表名的区别
python3.6和2.7的区别是什么
有关__FILE__和$_SERVER['SCRIPT_FILENAME']的区别

[关闭]