FBV + CBV

django中请求处理方式有2种:FBV 和 CBV

FBV(function bases views)

就是在视图里使用函数处理请求,如下:

# urls.py

from django.conf.urls import url, include
from app01 import views urlpatterns = [
url(r'^index/', views.index),
]
# views.py

from django.shortcuts import render

def index(req):
if req.method == 'POST':
print('method is :' + req.method)
elif req.method == 'GET':
print('method is :' + req.method)
return render(req, 'index.html')

注意此处定义的是函数【def index(req):】

<!--index.html-->

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>index</title>
</head>
<body>
<form action="" method="post">
<input type="text" name="A" />
<input type="submit" name="b" value="提交" />
</form>
</body>
</html>

FBV中加装饰器相关

def deco(func):
def wapper(request,*agrs,**kwargs):
if request.COOKIES.get('LOGIN'):
return func(request, *args, **kwargs)
return redirect('/login/')
return wrapper @deco
def index(req):
if req.method == 'POST':
print('method is :' + req.method)
elif req.method == 'GET':
print('method is :' + req.method)
return render(req, 'index.html')

上面就是FBV的使用。

CBV(class bases views)

就是在视图里使用类处理请求,如下:

# urls.py

from app01 import views

urlpatterns = [
url(r'^index/', views.Index.as_view()),
]
# views.py

from django.views import View

# 类要继承View ,类中函数名必须小写
class Index(View):
def get(self, req):
'''
处理GET请求
'''
print('method is :' + req.method)
return render(req, 'index.html') def post(self, req):
'''
处理POST请求
'''
print('method is :' + req.method)
return render(req, 'index.html')

CBV中加装饰器相关

要在CBV视图中使用我们上面的check_login装饰器,有以下三种方式:

  1. 加在CBV视图的get或post方法上

    from django.utils.decorators import method_decorator
    from django.views import View class Index(View): def dispatch(self,req,*args,**kwargs):
    return super(Index,self).dispatch(req,*args,**kwargs) def get(self, req):
    print('method is :' + req.method)
    return render(req, 'index.html') @method_decorator(deco)
    def post(self, req):
    print('method is :' + req.method)
    return render(req, 'index.html')
  2. 加在diapatch方法上,因为CBV中首先执行的就是dispatch方法,所以这么写相当于给get和post方法都加上了登录校验。

    from django.utils.decorators import method_decorator
    from django.views import View class Index(View): @method_decorator(deco)
    def dispatch(self,req,*args,**kwargs):
    return super(Index,self).dispatch(req,*args,**kwargs) def get(self, req):
    print('method is :' + req.method)
    return render(req, 'index.html') def post(self, req):
    print('method is :' + req.method)
    return render(req, 'index.html')
  3. 直接加在视图类上,但method_decorator必须传name关键字参数

    如果get方法和post方法都需要登录校验的话就写两个装饰器

    from django.utils.decorators import method_decorator
    from django.views import View @method_decorator(deco,name='get')
    @method_decorator(deco,name='post')
    class Index(View): def dispatch(self,req,*args,**kwargs):
    return super(Index,self).dispatch(req,*args,**kwargs) def get(self, req):
    print('method is :' + req.method)
    return render(req, 'index.html') def post(self, req):
    print('method is :' + req.method)
    return render(req, 'index.html')

Django--FBV + CBV的更多相关文章

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

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

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

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

  3. django FBV +CBV 视图处理方式总结

    1.FBV(function base views) 在视图里使用函数处理请求. url:        re_path('fbv', views.fbv),        # url(r'^fbv' ...

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

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

  5. django——FBV与CBV

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

  6. Django的CBV与FBV

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

  7. Django的 CBV和FBV

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

  8. Django 之 CBV & FBV

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

  9. Django之CBV和FBV

    Django之CBV和FBV CBV和FBV是C和F的区别: C是Class,F是Function 在请求中,有GET请求和POST请求. 在写CBV时,url是可以对应一个类的,在类中,分别写出GE ...

  10. Django 路由视图FBV/CBV

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

随机推荐

  1. react.js知识汇总

    首先ract的基本结构 var Input = React.createClass({ getInitialState: function() { return {value: 'Hello!'}; ...

  2. 【java】ORA-12505, TNS:listener does not currently know of SID given in connect descriptor

    如果是负载均衡,则 jdbc.url=jdbc:oracle:thin:@(description=(address_list= (address=(host=XX.XXX.X.XX) (protoc ...

  3. 洛谷 P4427

    传送门 洛谷P4427 题意: 给你一个数,然后让你求这两个数之间的点的深度的k次方和. #思路: 很容易想到lca.因为lca可以说是求树上两个点的距离的好方法.而且lca还能遍历每一个点. 然后我 ...

  4. ubuntu容器化开发系统配置 相关下载地址(laravel)

    ubuntu64位 18.04(开发使用桌面版本) Nginx PHP7.3 开发工具:PHPStorm 其他技术栈:laravel.VUE.NodeJs https://laravel.com/ h ...

  5. ECMAScript6-1

    1.let与const ES205(ES6)新增两个重要的JavaScript关键字:let和const let声明的变量只在let命令所在的代码块内有效,const声明一个只读的常量,一旦声明,其值 ...

  6. String.format()的详细用法

    问题 在开发的时候一段字符串的中间某一部分是需要可变的 比如一个Textview需要显示”XXX用户来自 上海 年龄 21 性别 男” 其中的 XXX 是用户名 每个用户也是不一样的 地区 上海 为可 ...

  7. 将物理机转换成vmware虚机

    随着虚拟化的快速发展,公司主要是以公有云+私有云结合的混合云部署,据我不成熟的了解,目前很少有公司会将一台单独的物理机作为服务器,在公司内部大家逐渐接受了私有云的部署方案,这样做不但可以节省硬件资源, ...

  8. how to compile and replace ubuntu kernel

    how to compile and replace ubuntu kernel 0. environment -ubuntu 1804 64bit 1. prepare source code su ...

  9. Ribbon核心组件IRule及配置指定的负载均衡算法

    Ribbon在工作时分为两步: 第一步:先选择 EurekaServer,它优先选择在同一个区域内负载较少的Server: 第二步:再根据用户指定的策略,在从Server取到的服务注册列表中选择一个地 ...

  10. kali 破解wifi

    1.查看网卡名称:iwconfig 有一块 名为wlan0 的无线网卡通过一下命令排除影响因素 root@jkali:~# iwconfig lo no wireless extensions. wl ...