zoukankan      html  css  js  c++  java
  • django视图系统

    request对象

    常用属性和方法

    print(request)  #wsgirequest对象
        print(request.path) #请求路径 #/index/
        print(request.method) #请求方法
        print(request.POST) #post请求提交的数据 <QueryDict: {'username': ['root']}>
        print(request.GET) # 获取url中的查询参数 <QueryDict: {'a': ['1'], 'b': ['2']}>  #不是针对get请求的
        print(request.body) #获取htt
        p请求消息格式的请求数据部分的内容  b''
        print(request.META) #请求头信息
        print(request.get_full_path()) #获取完整路径(包含查询参数的) /index/?a=1&b=3
    
        print(request.FILES) # 上传的文件对象数据
        print(request.FILES.get('file')) #26机试.pdf
        
        #<QueryDict: {'username': ['root'], 'sex': ['female'], 'hobby': ['2', '3']}>
        print(request.POST.get('username')) #zhen
        print(request.POST.get('gender')) #female
        # 多选提交来的数据通过getlist来获取
        print(request.POST.getlist('hobby')) #['2', '3']

    response响应

    常用方法

    from django.shortcuts import render, HttpResponse, redirect
    
    return HttpResponse('你好') #回复字符串
    return render(request,'home.html') #回复html页面
    
    #重定向方法,参数是个路径
    return redirect('/home/')  #封装了302状态码,以及浏览器要重定向的路径

    添加响应头键值对

    ret = render(request,'home.html') 
    ret['a'] = 'b' #添加响应头键值对
    
    return ret

    添加响应状态码

    ret = render(request,'home.html', status=202) #render修改状态码还可以这样改
    #ret['a'] = 'b' #添加响应头键值对
    ret.status_code = 201 #添加响应状态码
    return ret #回复html页面

    CBV和FBV

    两种视图逻辑的写法方法

    FBV:全称function based view,就是基于函数来写视图逻辑

    CBV:全称class based view,就是基于类来写视图

     

    基于函数的写法,写了很多了,这里不放示例了

    基于类的视图写法,如下,views.py文件

    from django.views import View
    
    #登录需求
    class LoginView(View):
        # get请求  获取login页面
        def get(self,request):
            return render(request,'login.html')
        # post请求,获取post请求提交的数据,并校验等等
    
        def post(self,request):
            print(request.POST)
            
            #<QueryDict: {'uname': ['chao'], 'pwd': ['123']}>
            return render(request,'login.html')

    url路径的写法:urls.py文件

    urlpatterns = [
        url(r'^admin/', admin.site.urls),
        url(r'^index/', views.index),
        url(r'^home/', views.home),
    
        # 类视图的url写法
        url(r'^login/', views.LoginView.as_view()),
    
    ]

    cbv源码重点(反射)

    from django.views import View
    View里面的dispatch方法中的反射逻辑,实现了不同的请求方法,找到我们视图类中的对应方法执行

    FBV和CBV加装饰器:三种方式

    FBV和普通函数加装饰器方式一样

    示例:

    #装饰器函数
    def outer(f):
        def inner(request, *args ,**kwargs):
            print('前面')
            ret = f(request, *args ,**kwargs)
            print('后面')
            return ret
        return inner
    
    #使用装饰器
    @outer
    def books(request):
        print('FBV执行了')
        return HttpResponse('book.html--ok')

    CBV加装饰器

    #装饰器函数
    def outer(f):
        def inner(request, *args ,**kwargs):
            print('前面')
            ret = f(request, *args ,**kwargs)
            print('后面')
            return ret
        return inner
    
    
    #使用装饰器
    #1 引入django提供的装饰器方法method_decorator来配合装饰器函数的使用
    from django.utils.decorators import method_decorator
    
    @method_decorator(outer,name='get') #CBV加装饰器方式3
    class BookView(View):
        #给类方法统一加装饰器,借助dispatch方法(父类的dispatch方法,就是通过反射来完成不同的请求方法找到并执行我们自己定义的视图类的对应方法)
        # 重写dispatch方法,dispatch方法是在其他请求方法对应的类方法执行之前执行的
        # @method_decorator(outer) #加装饰器方式2
        def dispatch(self, request, *args, **kwargs):
            # print('xxxxxx')
            ret = super().dispatch(request, *args, **kwargs)
            # print('oooooo')
            return ret
    
        #CBV加装饰器的方式1,给单独的方法加装饰器
        # @method_decorator(outer)
        def get(self, request, xx):
            print('CBV的get方法')
            return render(request, 'book.html')
    
        # @method_decorator(outer)
        def post(self,request, xx):
            print('CBV的post方法')
            return HttpResponse('ok')
  • 相关阅读:
    lambba表达式
    根据某个字段筛选list数据
    git和idea绑定
    gitee创建仓库
    用 Python 3 + PyQt5 擼了一個可以播放“任意”音樂的播放器
    zabbix 共享内存设置
    Zabbix高可用,实现zabbix的无缝切换,无故障时间
    python练习题100
    斐波那契数列python实现
    随机生成指定位数密码包括大小写数字特殊符号
  • 原文地址:https://www.cnblogs.com/fdsimin/p/13307007.html
Copyright © 2011-2022 走看看