Django学习笔记之Class-Based-View
作者:laixintao 时间:2023-09-07 15:37:25
前言
大家都知道其实学习Django非常简单,几乎不用花什么精力就可以入门了。配置一个url,分给一个函数处理它,返回response,几乎都没有什么很难理解的地方。
写多了,有些问题才逐渐认识到。比如有一个view比较复杂,调用了很多其他的函数。想要把这些函数封装起来,怎么办?当然,可以用注释#------view------这样将函数隔离开,这种方法太low了,简直是在骗自己,连封装都算不上。
Python是一个面向对象的编程语言,如果只用函数来开发,有很多面向对象的优点就错失了(继承、封装、多态)。所以Django在后来加入了Class-Based-View。可以让我们用类写View。这样做的优点主要下面两种:
提高了代码的复用性,可以使用面向对象的技术,比如Mixin(多继承)
可以用不同的函数针对不同的HTTP方法处理,而不是通过很多if判断,提高代码可读性
使用class-based views
如果我们要写一个处理GET方法的view,用函数写的话是下面这样。
from django.http import HttpResponse
def my_view(request):
if request.method == 'GET':
# <view logic>
return HttpResponse('result')
如果用class-based view写的话,就是下面这样。
from django.http import HttpResponse
from django.views import View
class MyView(View):
def get(self, request):
# <view logic>
return HttpResponse('result')
Django的url是将一个请求分配给可调用的函数的,而不是一个class。针对这个问题,class-based view提供了一个as_view()
静态方法(也就是类方法),调用这个方法,会创建一个类的实例,然后通过实例调用dispatch()
方法,dispatch()
方法会根据request的method的不同调用相应的方法来处理request(如get()
, post()
等)。到这里,这些方法和function-based view差不多了,要接收request,得到一个response返回。如果方法没有定义,会抛出HttpResponseNotAllowed异常。
在url中,就这么写:
# urls.py
from django.conf.urls import url
from myapp.views import MyView
urlpatterns = [
url(r'^about/$', MyView.as_view()),
]
类的属性可以通过两种方法设置,第一种是常见的Python的方法,可以被子类覆盖。
from django.http import HttpResponse
from django.views import View
class GreetingView(View):
greeting = "Good Day"
def get(self, request):
return HttpResponse(self.greeting)
# You can override that in a subclass
class MorningGreetingView(GreetingView):
greeting = "Morning to ya"
第二种方法,你也可以在url中指定类的属性:
在url中设置类的属性Python
urlpatterns = [
url(r'^about/$', GreetingView.as_view(greeting="G'day")),
]
使用Mixin
我觉得要理解django的class-based-view(以下简称cbv),首先要明白django引入cbv的目的是什么。在django1.3之前,generic view也就是所谓的通用视图,使用的是function-based-view(fbv),亦即基于函数的视图。有人认为fbv比cbv更pythonic,窃以为不然。python的一大重要的特性就是面向对象。而cbv更能体现python的面向对象。cbv是通过class的方式来实现视图方法的。class相对于function,更能利用多态的特定,因此更容易从宏观层面上将项目内的比较通用的功能抽象出来。关于多态,不多解释,有兴趣的同学自己Google。总之可以理解为一个东西具有多种形态(的特性)。cbv的实现原理通过看django的源码就很容易明白,大体就是由url路由到这个cbv之后,通过cbv内部的dispatch方法进行分发,将get请求分发给cbv.get方法处理,将post请求分发给cbv.post方法处理,其他方法类似。怎么利用多态呢?cbv里引入了mixin的概念。Mixin就是写好了的一些基础类,然后通过不同的Mixin组合成为最终想要的类。
所以,理解cbv的基础是,理解Mixin。Django中使用Mixin来重用代码,一个View Class可以继承多个Mixin,但是只能继承一个View(包括View的子类),推荐把View写在最右边,多个Mixin写在左边。Mixin也是比较复杂的技术,本文不详细说了,以后写一篇针对Mixin的文章吧。
使用装饰器
在CBV中,可以使用method_decorator来装饰方法。
from django.contrib.auth.decorators import login_required
from django.utils.decorators import method_decorator
from django.views.generic import TemplateView
class ProtectedView(TemplateView):
template_name = 'secret.html'
@method_decorator(login_required)
def dispatch(self, *args, **kwargs):
return super(ProtectedView, self).dispatch(*args, **kwargs)
也可以写在类上面,传入方法的名字。
@method_decorator(login_required, name='dispatch')
class ProtectedView(TemplateView):
template_name = 'secret.html'
如果有多个装饰器装饰一个方法,可以写成一个list。例如,下面这两种写法是等价的。
decorators = [never_cache, login_required]
@method_decorator(decorators, name='dispatch')
class ProtectedView(TemplateView):
template_name = 'secret.html'
@method_decorator(never_cache, name='dispatch')
@method_decorator(login_required, name='dispatch')
class ProtectedView(TemplateView):
template_name = 'secret.html'
总结
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
分析在Python中何种情况下需要使用断言
基于Python实现微博抓取GUI程序
![](https://img.aspxhome.com/file/2023/7/128257_0s.jpg)
Python编程学习之如何判断3个数的大小
Python logging日志库空间不足问题解决
![](https://img.aspxhome.com/file/2023/8/105348_0s.png)
CentOS6.4上使用yum安装mysql
![](https://img.aspxhome.com/file/2023/8/115278_0s.png)
Python爬取网易云音乐上评论火爆的歌曲
![](https://img.aspxhome.com/file/2023/1/92171_0s.png)
Python自动化办公之Word文档的创建与生成
![](https://img.aspxhome.com/file/2023/8/75908_0s.jpg)
sqlserver数据库主键的生成方式小结(sqlserver,mysql)
关于分页查询和性能问题
Pycharm最全报错的原因与解决方法总结(推荐!)
![](https://img.aspxhome.com/file/2023/4/79534_0s.jpg)
瞬间的设计(四)【碳酸饮料会】
![](https://img.aspxhome.com/file/UploadPic/20101/11/70618be7941ddd0f1be611656977bc15-93s.jpg)
在python代码中加入环境变量的语句操作
![](https://img.aspxhome.com/file/2023/0/97050_0s.jpg)
python 处理dataframe中的时间字段方法
FF下,用 col 隐藏表格列的方法详解!
Python机器学习应用之基于天气数据集的XGBoost分类篇解读
![](https://img.aspxhome.com/file/2023/1/128521_0s.png)
Python list运算操作代码实例解析
Javascript的一种模块模式
python字符串运算符详情
![](https://img.aspxhome.com/file/2023/9/64609_0s.png)
Python 处理带有 \\u 的字符串操作
![](https://img.aspxhome.com/file/2023/7/79397_0s.jpg)
python因子分析的实例
![](https://img.aspxhome.com/file/2023/8/76738_0s.png)