1、FBV(function base views)

在视图里使用函数处理请求。

url:
        re_path('fbv', views.fbv),
        # url(r'^fbv', views.fbv),

func:
        def fbv(requset):
                return render(requset,'fbv_Cbv.html')

2、CBV (class base views)

url:

re_path('cbv', views.Cbv.as_view()),
# url(r'^cbv', views.Cbv.as_view()),

class:

from django.views import View
#导入View模块
class Home(View):  #使用类处理需要继承View(view是Home的父类,Home是子类)  
        def get(self,request): #自动识别,如果用户请求方式:get,那么自动调用该方法执行结果
                print(request.method)
                return render(request, 'home.html')

def post(self,request):
             print(request.method)#自动识别,如果用户请求方式:post,那么自动调用该方法执行结果
            return render(request, 'home.html')

3、FBV+CBV 获取数据、返回数据的命令参数

    A.获取数据的几种方式
        request.method
        request.GET
        request.POST
        request.FILES
        request.path_info
        request.COOKIES
        reqeust.body    #所有内容的原生数据

  B.获取checkbox等多选的内容
        request.POST.getlist()
        request.GET.getlist()

#所以分成两类
        request.body  #用户请求的原生数据,以字符串形式存放

request.PUT
        request.DELECT
        request.MOVE
       #django对上面3种没有做处理,所以我们操作以上3种的时候就需要通过request.body获取原生数据:字符串形式

request.Meta
       #出了数据外,请求头相关的信息,如:判断用户是PC端还是移动端

request.method(POST、GET、PUT..)
       request.path_info
       request.COOKIES

  C.接收上传文件,注意:模板文件html的form标签必须做特殊设置:enctype="multipart/form-data"
        obj = request.FILES.get('file')
        obj.name    #取该文件名
        obj.size       #取该文件的字节大小
        obj.chunks  #取该文件的块

file_path = os.path.join('%s\\upload'%os.path.dirname(os.path.abspath(__file__)), obj.name)  #当前文件的目录下的upload目录和接收文件名进行拼接成文件路径
        f = open(obj.name,mode= 'wb') #如果该路径文件存在就打开,不存在就创建
        for item in obj.chunks():
            f.write(item) #写入文件
        f.close()     #关闭文件
            
 D、返回给用户的几种方式  

return render(request,'模板路径.html',{'list':[1,2,3,4],'dict': {'k1':'v1','k2':'v2'} })  #返回给用户指定经过模板渲染后的html
        retune redirect(’url路径’)        #跳转到指定url
        retune HttpResponse(‘字符串’)  #直接返回给用户字符串

response = HttpResponse(a)
       response.set_cookie(‘key’:’value’)  #设置客户端cookie
       response[‘name’] = ‘bur’   #设置客户端响应头
       return response

4、FBV+CBV 添加验证装饰器

A、FBV添加装饰器

def auth(func):
def deco(request, *args, **kwargs):
u = request.get_signed_cookie('username', salt='user', default=None)
if not u:
return render(request, 'login.html')
return func(request, *args, **kwargs)
return deco @auth
def index(request):
u = request.get_signed_cookie('username', salt='user', default=None)
return render(request, 'index.html', {'user': u}) @auth
def detail(request):
u = request.get_signed_cookie('username', salt='user', default=None)
return render(request, 'detail.html', {'user': u}) 访问index/detail时,调用auth装饰器,如果验证成功,则执行index/detail(return func(request, *args, **kwargs)语句起的作用);
否则跳转到login.html

B、CBV方式添加装饰器:通过django自带的装饰器method_decorator 的@method_decorator(cookie)来实现

from django.utils.decorators import method_decorator
from django import views # @method_decorator(cookie,name='dispatch') # dispatch的便捷写法
class CBVtest(views.View):
@method_decorator(cookie) # 给dispatch方法添加装饰器,那么下面所有的get,post都会添加
def dispatch(self, request, *args, **kwargs):
return super(CBVtest, self).dispatch(request, *args, **kwargs) # @method_decorator(cookie) # 单独添加
def get(self, request):
u = request.get_signed_cookie('username', salt='user', default=None)
return render(request, 'houtai.html', {'user': u}) def post(self, request):
return HttpResponse('post ok')

django FBV +CBV 视图处理方式总结的更多相关文章

  1. Django FBV CBV以及使用django提供的API接口

    FBV 和 CBV 使用哪一种方式都可以,根据自己的情况进行选择 看看FBV的代码 URL的写法: from django.conf.urls import url from api import v ...

  2. Django FBV/CBV、中间件、GIT使用

    s5day82 内容回顾: 1. Http请求本质 Django程序:socket服务端 a. 服务端监听IP和端口 c. 接受请求 \r\n\r\n:请求头和请求体 \r\n & reque ...

  3. [oldboy-django][2深入django]FBV + CBV + 装饰器

    FBV django CBV & FBV - FBV function basic view a. urls 设置 urls(r'^test.html$', views.test) b. vi ...

  4. Django之CBV视图源码分析(工作原理)

    1.首先我们先在urls.py定义CBV的路由匹配. FBV的路由匹配: 2.然后,在views.py创建一名为MyReg的类: 注意:该类必须继续View类,且方法名必须与请求方式相同(后面会详解) ...

  5. Django 路由视图FBV/CBV

    路由层  url路由层结构 from django.conf.urls import url from django.contrib import admin from app01 import vi ...

  6. django——FBV与CBV

    引言 FBV FBV(function base views) 就是在视图里使用函数处理请求. 在之前django的学习中,我们一直使用的是这种方式,所以不再赘述. CBV CBV(class bas ...

  7. Django的CBV与FBV

    FBV FBV(function base views) 就是在视图里使用函数处理请求. 在之前django的学习中,我们一直使用的是这种方式,所以不再赘述. CBV CBV(class base v ...

  8. Django的 CBV和FBV

    FBV CBV 回顾多重继承和Mixin 回到顶部 FBV FBV(function base views) 就是在视图里使用函数处理请求. 在之前django的学习中,我们一直使用的是这种方式,所以 ...

  9. Django 之 CBV & FBV

    FBV FBV(function base views) 就是在视图里使用函数处理请求. 在之前django随笔中,一直使用的是这种方式,不再赘述. CBV CBV(class base views) ...

随机推荐

  1. python深度学习培训概念整理

    对于公司组织的人工智能学习,每周日一天课程共计五周,已经上了三次,一天课程下来讲了两本书的知识.发现老师讲的速度太快,深度不够,而且其他公司学员有的没有接触过python知识,所以有必要自己花时间多看 ...

  2. Centos7 搭建pptp服务器

    1.检查是否支持pptp 返回ok即表示支持 modprobe ppp-compress-18 && echo ok 2.安装ppp yum install -y ppp 3.安装pp ...

  3. 嘉馨学姐又双叒叕来吃包子了 QDUOJ 模拟 尺度法

    嘉馨学姐又双叒叕来吃包子了 QDUOJ 模拟 尺度法 点我进入OJ题目详情 题意 给你一串数,让你求长度最长的子串,这个字串满足里面没有重复出现的数字. 解题思路 使用一个标记数组,来标记每个数的第一 ...

  4. TypeScript ES6-Promise 递归遍历文件夹中的文件

    貌似很多人都爱用这个作为写文章的初尝试,那来吧.遍历文件夹下的所有文件,如遍历文件夹下并操作HTML/CSS/JS/PNG/JPG步骤如下:1.传入一个路径,读取路径里面所有的文件:2.遍历读取的文件 ...

  5. RMQ 模板

    RMQ(Range Minimum/Maximum Query),即区间最值查询,是指这样一个问题:对于长度为 n的数列A,回答若干次询问RMQ(i,j),返回数列A中下标在区间[i,j]中的最小/大 ...

  6. 攻防世界--when_did_you_born5

    测试文件:https://adworld.xctf.org.cn/media/task/attachments/24937e95ca4744818feebe82ab96902d 1.准备 root@l ...

  7. k3 cloud中单据体中文本自适应

    在单据体中添加多行文本,然后设置本地配置,只读单元格自动换行

  8. APP元素定位工具weditor

    github地址https://github.com/openatx/weditor python -m weditor --shortcut adb devices 在页面上输入手机设备号,点Con ...

  9. Java的volatile

    1.同步 同synchronized相比(synchronized通常称为重量级锁),volatile更轻量级 如图,如果变量没有volatile关键字,那么A线程对该变量的改变存储在内存A,B变量不 ...

  10. nginx之热部署,以及版本回滚

    热部署的概念:当从老版本替换为新版本的nginx的时候,如果不热部署的话,会需要取消nginx服务并重启服务才能替换成功,这样的话会使正在访问的用户在断开连接,所以为了不影响用户的体验,且需要版本升级 ...