Django中的CBV和FBV示例介绍
作者:xsan 时间:2022-05-23 10:13:59
前言
本文主要给大家介绍了关于Django中CBV和FBV的相关内容,分享出来供大家参考学习,下面话不多说了,来一起看看详细的介绍吧。
一、 CBV
CBV是采用面向对象的方法写视图文件。
CBV的执行流程:
浏览器向服务器端发送请求,服务器端的urls.py根据请求匹配url,找到要执行的视图类,执行dispatch方法区分出是POST请求还是GET请求,执行views.py对应类中的POST方法或GET方法。
使用实例:
urls.py
path('login/',views.Login.as_view())
views.py
from django import views #在views.py的基础上添加
class Login(views.Views):
def get(self,request)
pass
def pass(self,request)
pass
使用装饰器:
from django import views
from django.utils.decorators import method_decorator
def outer(func):
def inner(request,*args,**kwargs):
return func(request,*args,**kwargs)
return inner
class Login(views.View):
@method_decorator(outer)
def get(self,request,*args,**kwargs):
pass
在类上面加装饰器,和在函数上加装饰器是一个性质。但加的方法有所不同。
eg:
@method_decorator(outer,name='dispatch')
class Login(views.View):
自定义dispatch:
class Login(views.View):
def dispatch(self, request, *args, **kwargs):
print(2222)
ret = super(Login, self).dispatch(request, *args, **kwargs)
print(1111)
return ret
def get(self, request, *args, **kwargs):
print('GET')
return HttpResponse('OK')
执行结果:2222
GET
1111
二、 FBV
FBV即在views.py中以函数的形式写视图。
看代码:
urls.py
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
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的使用。
来源:http://www.cnblogs.com/xshan/p/8467763.html
标签:django,cbv,fbv
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
利用ThinkPHP内置的ThinkAjax实现异步传输技术的实现方法
2023-09-11 15:11:50
Go的固定时长定时器和周期性时长定时器
2023-06-27 12:44:23
Oracle临时表空间删除和重建实现过程
2023-07-18 09:26:04
Python入门篇之字典
2022-09-24 23:53:47
Python读取大型数据文件的6种方式汇总
2021-06-29 12:19:33
python matplotlib中文显示参数设置解析
2022-11-18 14:11:41
![](https://img.aspxhome.com/file/2023/0/89410_0s.jpg)
Python语音合成之第三方库gTTs/pyttsx3/speech横评(内附使用方法)
2022-03-23 08:31:22
【启发】不用循环计算1到100的和
2009-03-04 12:16:00
php生成随机数/生成随机字符串的方法小结【5种方法】
2023-09-05 20:23:21
opencv python统计及绘制直方图的方法
2021-12-27 21:52:45
![](https://img.aspxhome.com/file/2023/0/81450_0s.png)
php之php.ini配置文件讲解案例
2023-06-11 18:19:06
基于Python制作一个汇率换算程序
2022-05-25 20:33:25
Python类和实例的属性机制原理详解
2022-12-12 19:47:27
Oracle DBA常用语句
2009-08-05 20:15:00
python多线程分块读取文件
2023-10-29 18:48:51
Python实现快速傅里叶变换的方法(FFT)
2022-09-18 07:21:47
![](https://img.aspxhome.com/file/2023/3/77873_0s.png)
经典分享MySQL的limit查询优化
2011-05-05 15:47:00
python中startswith()和endswith()的用法详解
2023-11-02 12:41:09
css3弹性盒模型
2010-05-10 20:47:00
![](https://img.aspxhome.com/file/UploadPic/20105/10/01-33s.jpg)
基于Python代码实现Apriori 关联规则算法
2021-01-25 11:53:45
![](https://img.aspxhome.com/file/2023/1/87711_0s.jpg)