Django【进阶】FBV 和 CBV
django中请求处理方式有2种:FBV 和 CBV
一、FBV
FBV(function base views) 就是在视图里使用函数处理请求。
看代码:
urls.py
|
1
2
3
4
5
6
7
8
|
fromdjango.conf.urlsimporturl, include# from django.contrib import adminfrommytestimportviewsurlpatterns=[ # url(r‘^admin/‘, admin.site.urls), url(r‘^index/‘, views.index),] |
views.py
|
1
2
3
4
5
6
7
8
9
|
fromdjango.shortcutsimportrenderdefindex(req): ifreq.method==‘POST‘: print(‘methodis:‘+req.method) elifreq.method==‘GET‘: print(‘methodis:‘+req.method) returnrender(req, ‘index.html‘) |
注意此处定义的是函数【def index(req):】
index.html
|
1
2
3
4
5
6
7
8
9
10
11
12
13
|
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title>index</title></head><body> <form action="" method="post"> <inputtype="text"name="A"/> <inputtype="submit"name="b"value="提交"/> </form></body></html> |
上面就是FBV的使用。
二、CBV
1、CBV(class base views) 就是在视图里使用类处理请求。


分别处理get和post请求
get和post方法是如何被调用的?????
实际上父类View中有一个dispatch方法,作用就是通过反射来调用子类的get和post方法。
请求先走dispatch,res就是get方法或者post方法执行只有的结果
所以这个请求的过程是:请求--->dispatch--->get/post
我们现在把dispatch写到子类中,继承父类的dispatch方法。dispatch写到子类或者单独写一个类,目的是根据需求加功能。

2、我现在想实现验证登录的功能(用CBV实现登录验证不如用中间件,所以我们一般用中间件来实现验证功能)
下面的函数是实现登录的
def dispatch(self, request, *args, **kwargs):
return super(LoginView,self).dispatch(request, *args, **kwargs) def get(self,request):
print('login')
return render(request,'login.html') def post(self,request):
# request.GET
# request.POST # 请求头中的:content-type
# 注意:request.POST中的数据是request.body中转换过来的,可能为空,因为可能转换会不成功
# request.body 但凡以post提交数据,request.body中一定有值
user = request.POST.get('user')
pwd = request.POST.get('pwd')
if user == 'alex' and pwd == "alex3714": # 生成随机字符串
# 写浏览器cookie: session_id: 随机字符串
# 写到服务端session:
# {
# "随机字符串": {'user_info':'alex}
# }
request.session['user_info'] = "alex" # 这个代码有上面注释的几个操作 return redirect('/index.html')
return render(request, 'login.html')

class AuthView(object):
def dispatch(self, request, *args, **kwargs):
if not request.session.get('user_info'):
return redirect('/login.html')
res = super(AuthView,self).dispatch(request, *args, **kwargs)
return res class IndexView(AuthView,View):
def get(self,request,*args,**kwargs):
return render(request,'index.html') def post(self,request,*args,**kwargs):
return HttpResponse('999') class OrderView(AuthView,View):
def get(self,request,*args,**kwargs):
return render(request,'index.html') def post(self,request,*args,**kwargs):
return HttpResponse('999')
加装饰的格式@method_decorator(test),test是装饰器函数
def test(func):
def inner(*args,**kwargs):
return func(*args,**kwargs)
return inner
@method_decorator(test,name='get')
class LoginView(View):
可以加到dispatch方法,也可以加到get或post方法,不需要传name="方法名"
@method_decorator(test)
def dispath(self,request,*args,**kwargs):
4、特殊装饰器:CSRF Token只能加到dispatch(django的bug)
CBV的csrf装饰器需要导入
from django.views.decorators.csrf import csrf_exempt,csrf_protect
@method_decorator(csrf_exempt)
def dispatch(self, request, *args, **kwargs):
return super(LoginView,self).dispatch(request, *args, **kwargs)
Django【进阶】FBV 和 CBV的更多相关文章
- Django之FBV与CBV
一.FBV与CBV FBV(function based views),即基于函数的视图:CBV(class based views),即基于类的视图,也是基于对象的视图.当看到这个解释时,我是很萌的 ...
- django的FBV和CBV
title: python djano CBV FBV tags: python, djano, CBV, FBV grammar_cjkRuby: true --- python django的fu ...
- django的FBV和CBV的装饰器例子
备忘 def auth(func): def inner(request,*args,**kwargs): u = request.COOKIES.get('username111') if not ...
- Django之FBV和CBV的用法
FBV FBV,即 func base views,函数视图,在视图里使用函数处理请求. 以用户注册代码为例, 使用两个函数完成注册 初级注册代码 def register(request): &qu ...
- Django的FBV和CB
Django的FBV和CBV FBV FBV(function base views) 就是在视图里使用函数处理请求. 在之前django的学习中,我们一直使用的是这种方式,所以不再赘述. CBV C ...
- django请求生命周期,FBV和CBV,ORM拾遗,Git
一.django 请求生命周期 流程图: 1. 当用户在浏览器中输入url时,浏览器会生成请求头和请求体发给服务端请求头和请求体中会包含浏览器的动作(action),这个动作通常为get或者post, ...
- django——FBV与CBV
引言 FBV FBV(function base views) 就是在视图里使用函数处理请求. 在之前django的学习中,我们一直使用的是这种方式,所以不再赘述. CBV CBV(class bas ...
- python 全栈开发,Day84(django请求生命周期,FBV和CBV,ORM拾遗,Git)
一.django 请求生命周期 流程图: 1. 当用户在浏览器中输入url时,浏览器会生成请求头和请求体发给服务端请求头和请求体中会包含浏览器的动作(action),这个动作通常为get或者post, ...
- django基础 -- 4. 模板语言 过滤器 模板继承 FBV 和CBV 装饰器 组件
一.语法 两种特殊符号(语法): {{ }}和 {% %} 变量相关的用{{}},逻辑相关的用{%%}. 二.变量 1. 可直接用 {{ 变量名 }} (可调用字符串, 数字 ,列表,字典,对象等) ...
随机推荐
- ZooKeeper的伪分布式集群搭建
ZooKeeper集群的一些基本概念 zookeeper集群搭建: zk集群,主从节点,心跳机制(选举模式) 配置数据文件 myid 1/2/3 对应 server.1/2/3 通过 zkCli.sh ...
- Android之内容提供者ContentProvider的总结
本文包含以下知识点: ContentProvider Uri 的介绍 ContentResolver: 监听ContentProvider的数据改变 一:ContentProvider部分 Conte ...
- URAL 1932 The Secret of Identifier(容斥)
Description Davy Jones: You've been captain of the Black Pearl for 13 years. That was our agreement. ...
- 字面值常量&&转义序列
字面值常量举例: 字面值常量的分类 示例 备注 整型 42.024(八进制数).0x23(十六进制) short类型没有对应的字面值 浮点型 3.14.3.14E2(指数) 默认类型是double 字 ...
- mysql数据库,编码错误解决
在写代码的过程中,经常会遇见,将中文字符输入到mysql数据库中,但是查看的时候,却发现,中文显示为乱码的情况,让人相当的头疼,今天正好解决了一个这样遇到的问题,所以简单总结一下: 1.首先查看数据库 ...
- 【Linux】Linux修改openfiles后不生效问题?
#次故障问题环境背景: Centos7.4物理机,升级过ssh和ntp: #一般只需要在此文件后面添加4行就行,配置后即可生效(exit再次登录即可生效),此次配置后没生效,reboot还是没生效,在 ...
- 推荐一个好的Redis GUI 客户端工具
推荐一个好的Redis GUI 客户端工具 Redis Desktop Manager
- 【python】python 中的三元表达式(三目运算符)
python中的三目运算符不像其他语言其他的一般都是 判定条件?为真时的结果:为假时的结果 如 result=5>3?1:0 这个输出1,但没有什么意义,仅仅是一个例子.而在python中的格式 ...
- adoop集群动态添加和删除节点
hadoop集群动态添加和删除节点说明 上篇博客我已经安装了Hadoop集群(hadoop集群的安装步骤和配置),现在写这个博客我将在之前的基础上进行节点的添加的删除. 首先将启动四台机器(一主三从) ...
- 【bzoj3043】IncDec Sequence 差分
题目描述 给定一个长度为n的数列{a1,a2...an},每次可以选择一个区间[l,r],使这个区间内的数都加一或者都减一.问至少需要多少次操作才能使数列中的所有数都一样,并求出在保证最少次数的前提下 ...