Django--FBV + CBV
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装饰器,有以下三种方式:
加在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')加在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')直接加在视图类上,但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的更多相关文章
- Django FBV/CBV、中间件、GIT使用
s5day82 内容回顾: 1. Http请求本质 Django程序:socket服务端 a. 服务端监听IP和端口 c. 接受请求 \r\n\r\n:请求头和请求体 \r\n & reque ...
- Django FBV CBV以及使用django提供的API接口
FBV 和 CBV 使用哪一种方式都可以,根据自己的情况进行选择 看看FBV的代码 URL的写法: from django.conf.urls import url from api import v ...
- django FBV +CBV 视图处理方式总结
1.FBV(function base views) 在视图里使用函数处理请求. url: re_path('fbv', views.fbv), # url(r'^fbv' ...
- [oldboy-django][2深入django]FBV + CBV + 装饰器
FBV django CBV & FBV - FBV function basic view a. urls 设置 urls(r'^test.html$', views.test) b. vi ...
- django——FBV与CBV
引言 FBV FBV(function base views) 就是在视图里使用函数处理请求. 在之前django的学习中,我们一直使用的是这种方式,所以不再赘述. CBV CBV(class bas ...
- Django的CBV与FBV
FBV FBV(function base views) 就是在视图里使用函数处理请求. 在之前django的学习中,我们一直使用的是这种方式,所以不再赘述. CBV CBV(class base v ...
- Django的 CBV和FBV
FBV CBV 回顾多重继承和Mixin 回到顶部 FBV FBV(function base views) 就是在视图里使用函数处理请求. 在之前django的学习中,我们一直使用的是这种方式,所以 ...
- Django 之 CBV & FBV
FBV FBV(function base views) 就是在视图里使用函数处理请求. 在之前django随笔中,一直使用的是这种方式,不再赘述. CBV CBV(class base views) ...
- Django之CBV和FBV
Django之CBV和FBV CBV和FBV是C和F的区别: C是Class,F是Function 在请求中,有GET请求和POST请求. 在写CBV时,url是可以对应一个类的,在类中,分别写出GE ...
- Django 路由视图FBV/CBV
路由层 url路由层结构 from django.conf.urls import url from django.contrib import admin from app01 import vi ...
随机推荐
- 201871010118-唐敬博《面向对象程序设计(Java)》第二周学习总结
博文正文开头格式:(3分) 项目 内容 这个作业属于哪个课程 https://www.cnblogs.com/nwnu-daizh/ 这个作业的要求在哪里 https://www.cnblogs.co ...
- ORM基本增删改查
将上一节建造好的数据库 引入Navicate copy_path找到数据表路径 再用navicat打开 创建好的db不只一张表,其中第一张是我们创建的可以用的表 ,其他都是django自带功能所需表 ...
- 【oracle】截取字符串
select id,name,substr(dept,1,2) from test; 提取字段dept前两位 substr(string,start,length)
- 转:ubuntu16安装python3.6并将环境设置为系统默认
按照本文升级到3.6,但pip无法运行了,问题没有解决 1.添加python3.6安装包,并且安装 sudo apt-get install software-properties-common su ...
- 笔记&想要置顶但懒得置顶的文章目录
1.\(\text{Markdown&Latex}\)学习笔记 2.一些笔记 3.好东西 4.线段树学习笔记 5.并查集学习笔记 6.友链 7.语录
- 【DP】【P5615】 [MtOI2019] 时间跳跃
Description 给定 \(n\) 条边,第 \(i\) 条边的长度为 \(i\),每条边都有 \(50\%\) 的概率被选择,求如果选出的边能组成一个平面凸多边形,则方案的权值是方案中边的数量 ...
- CSRF 跨站
目录 CSRF 跨站请求伪造 解决跨站伪造问题: csrf 相关的装饰器: csrf.js文件: CSRF 跨站请求伪造 CSRF全称为Cross-site request forgery,也被称为: ...
- 自动签发https证书工具 cert manager
最近cert manager进行升级,不再支持0.11以下的版本了,所以进行升级.但是发现不能直接通过更改镜像版本来升级,在Apps里的版本也是旧版本,部署后发现不支持,于是自已动手,根据文档整理了一 ...
- Notepad++使用NppFTP插件编辑linux上的文件
[转载请注明]: 原文出处:https://www.cnblogs.com/jstarseven/p/11351446.html 作者:jstarseven 码字挺辛苦的..... 一.N ...
- jquery的Layer弹出框操作
在layer中,我们要先获取窗口的索引,然后再进行操作. var index = parent.layer.getFrameIndex(window.name); //获取窗口索引 $("# ...