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 ...
随机推荐
- 一探istio-proxy(envoy)容器里的秘密
今天,在测试环境跑通了istio. 惭愧,是用MicroK8s跑的,其它环境,不敢呀. 基本功能都OK了. 在运行了istio-injection=enabled之后,每个pod运行时,会多一个ist ...
- HDU6583:Typewriter(dp+后缀自动机)
传送门 题意: 给出\(p,q\),现在要你生成一个字符串\(s\). 你可以进行两种操作:一种是花费\(p\)的代价随意在后面添加一个字符,另一种是花费\(q\)的代价可以随意赋值前面的一个子串. ...
- JS高阶---简介+数据类型
首先看下大概流程 [一]基础 接下来看下数据类型分类和判断 (1)数据类型分类 基本类型/值类型5种 ---字符串String.数字Number.布尔值Boolean.未定义undefined.空nu ...
- Java多线程编程核心技术-第6章-单例模式与多线程-读书笔记
第 6 章 单例模式与多线程 本章主要内容 如何使单例模式遇到多线程是安全的.正确的. 6.1 立即加载 / “饿汉模式” 什么是立即加载?立即加载就是使用类的时候已经将对象创建完毕,常见的实现办法就 ...
- PHP将图片转base64格式函数
base64的好处是什么?今天在跟小伙伴讨论这个问题,要是全站用Php把图片转为base64行不行? 1. 提升性能: 网页上的每一个图片,都是需要消耗一个 http 请求下载而来的, 图片的下载始终 ...
- 2019徐州网络赛H :function (min25筛)
题意:f(i)=i的幂次之和. 求(N+1-i)*f(i)之和. 思路:可以推论得对于一个素数p^k,其贡献是ans=(N+1)[N/(P^k)]+P^k(1+2+3...N/(P^k)); 我们分两 ...
- 三块sm865组建RAID0
介绍 使用三块sm865组件raid0,cpu为6700k,主板为华硕的z170-A 这里使用的是主板自带的raid0,不是win10自带的带区卷 CrystalDiskMark AS SSD Ben ...
- python基础之二:占位符、格式化输出、while else 、逻辑运算
1.占位符和格式化输出 示例代码 #格式化输出 # % s d # name = input('请输入姓名') # age = input('请输入年龄') # height = input('请输入 ...
- 破解优酷VIP视频
目录 一 破解优酷VIP视频 一 破解优酷VIP视频 import requests import re import json HEADERS = { 'user-agent': 'Mozilla/ ...
- [RN] React Native 使用 react-native-camera 过程中报错 Found react-native-camera 'mlkit' but wasn't required.`
详细报错如下: Could not resolve all task dependencies for configuration ':app:debugRuntimeClasspath'. Coul ...