zoukankan      html  css  js  c++  java
  • django2与1的差别和视图

    django1与2路由的差别

    在django1中的url在django2中为re_path
    django2中新增了path
        1.from django.urls import path
        2.不支持正则,精准匹配
        3.5个转换器(int,str,slug,path,uuid4.自定义转换器:
            1 写一个类:
    		class Test:
    			regex = '[0-9]{4}'
    			def to_python(self, value):
    				# 写一堆处理
    				value=value+'aaa'
    				return value
    			def to_url(self, value):
    				return '%04d' % 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
    

    三件套

    render
    HttpResponse
    redirect
    

    JsonResponse

    向前端页面发json格式字符串
    封装了json
    from django.http import JsonResponse
    
    dic={'name':'lqz','age':18}
    li=[1,2,3,4]
    
    return JsonResponse(li,safe=False)
    

    CBV和FBV

    CBVclass base view)和FBV(function base view)
    
    cbv:
    
    from django.views import View
    class Test(View):
    	def dispatch(self, request, *args, **kwargs):   # 分发
    		# 可加逻辑判断语句
    		obj=super().dispatch(request, *args, **kwargs)
    		# 可加逻辑判断语句
    		return obj
    	def get(self,request):
    		obj= render(request,'index.html')
    		print(type(obj))
    		return obj
    	def post(self,request):
    		return HttpResponse('ok')
    
    urls中:
    re_path(r'index/', views.Test.as_view()),
    

    简单文件上传

    html中:
    <form action="" method="post" enctype="multipart/form-data">
    用户名:<input type="text" name="name">
    密码:<input type="text" name="password">
    文件:<input type="file" name="myfile">
    <input type="submit">
    </form>
    
    viewsclass Cbv(View):
        def dispatch(self, request, *args, **kwargs):
            obj = super().dispatch(request, *args, **kwargs)
            return obj
    
        def get(self,request):
            return render(request,'index.html')
    
        def post(self,request):
            aaa = request.FILES.get('myfile')
            with open(aaa.name,'wb') as f:
                for line in aaa.chunks():
                    f.write(line)
            return HttpResponse('ok')
  • 相关阅读:
    mysql-Invalid use of group function-聚合函数不能直接使用在where后面-使用exists,外查询与子查询应条件关联
    python-数据库之pymysql模块(连接对象-游标对象-执行-获取值或者提交事务)
    python作业-99乘法表作业,注意制表符合print结束符以及外层和里层怎么确定的,以及闰年
    python学习笔记-if_while_for_break_continue
    python-python中的Debug大法
    python-常用的几种格式化方法
    python学习一周总结
    python学习笔记-列表,字典,以及函数返回值
    python-注释,变量命名和规范笔记
    OpenJudge 求重要逆序对数
  • 原文地址:https://www.cnblogs.com/luck-L/p/9595448.html
Copyright © 2011-2022 走看看