Django【进阶】FBV 和 CBV
2024-09-04 16:04:43
django中请求处理方式有2种:FBV 和 CBV
一、FBV
FBV(function base views) 就是在视图里使用函数处理请求。
看代码:
urls.py
1
2
3
4
5
6
7
8
|
from django.conf.urls import url, include # from django.contrib import admin from mytest import views urlpatterns = [ # url(r‘^admin/‘, admin.site.urls), url(r‘^index / ‘, views.index), ] |
views.py
1
2
3
4
5
6
7
8
9
|
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
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"> < input type = "text" name = "A" / > < input type = "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')
把dispatch拿出来单独写一个类,这个类提供验证登录的功能,让其他类来继承,如下:
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')
3、对于CBV加装饰器的方法
如果加装饰器,需要导入method_decorator
加装饰的格式@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
csrf_exempt是全局需要,唯独这个不需要
csrf_protect是全局不需要,唯独这个需要
@method_decorator(csrf_exempt)
def dispatch(self, request, *args, **kwargs):
return super(LoginView,self).dispatch(request, *args, **kwargs)
最新文章
- Python的多线程(threading)与多进程(multiprocessing )
- shell循环语句
- AB窗体互传参数
- 21-React的学习
- Win7虚拟机无法打开内核设备:\\Global\\vmx86
- MYSQL游标使用
- document.all的详细解释(document.all基本上所有浏览器可用!)
- Apache配置虚拟主机后,不能访问localhost的问题
- Visual Studio的.NET内存分配分析器解析
- JavaScript高级程序设计23.pdf
- Object-C @synthesize -- 笔记
- php用get_meta_tags轻松获取网页的meta信息
- c#关键字和常用类型表快查
- 【XSY2771】城市 分治
- MySQL会发生死锁吗?
- 前端解读控制反转(IOC)
- ios-UITableView无内容时,不显示多余的分隔线
- springMVC访问 WEB-INF 下的 jsp 和 html
- LightOJ 1030 - Discovering Gold - [概率DP]
- Luogu 1220 关路灯(动态规划)