FBV即以函数的形式实现视图函数,CBV即以类的形式实现视图函数;相比而言,CBV根据请求方式书写各自的代码逻辑,结构清晰明了,但是由于多了一层反射机制,性能要差一些;FBV执行效率要高一些,但是代码逻辑看起来要混乱一些。

一、CBV源码实现

  django支持以类的形式写视图函数,它需要继承自django.views.generic.base.View。可以通过from django.views.gener

class View:
"""
Intentionally simple parent class for all views. Only implements
dispatch-by-method and simple sanity checking.
"""
  # 1.允许可被重写的方法列表
http_method_names = ['get', 'post', 'put', 'patch', 'delete', 'head', 'options', 'trace'] def __init__(self, **kwargs):
"""
Constructor. Called in the URLconf; can contain helpful extra
keyword arguments, and other things.
"""
# Go through keyword arguments, and either save their values to our
# instance, or raise an error.
for key, value in kwargs.items():
setattr(self, key, value)
  # 2.在path路径中将views.funciton改写为views.Class.as_view(),其实是执行了这个函数;initkwaegs可以写入传递的请求方式;
  """
    在views.Class.as_view()时,首先把cls,也就是自己传递给了这个函数;
    定义了view函数后,给view函数设置view_class和view_initkwargs属性;
    将view函数作为装饰器,装饰给这个cls,以及cls中的dispatch方法;其本质上就是将initkwarga传递进去;
    真正返回的是view函数,它内部实例化了当前的cls类,可以使用cls中的方法;
    整个过程相当于调用了View.view方法,只不过对这个方法做了一下包裹.
    感觉像是一个人模型(Class)在空间戒指(as_view)隐藏了自身的属性(view_class和view_initkwargs)和功能(view)。
  """

@classonlymethod
def as_view(cls, **initkwargs):
"""Main entry point for a request-response process."""
for key in initkwargs:
if key in cls.http_method_names:
raise TypeError("You tried to pass in the %s method name as a "
"keyword argument to %s(). Don't do that."
% (key, cls.__name__))
if not hasattr(cls, key):
raise TypeError("%s() received an invalid keyword %r. as_view "
"only accepts arguments that are already "
"attributes of the class." % (cls.__name__, key))
     # 3.path调用views.Class.as_view(),也就是执行view函数;view函数实例化了一个当前Class的对象,并把request以及参数封装传递给了Class.dispatch,然后调用Class.dispatchdef view(request, *args, **kwargs):
self = cls(**initkwargs)
if hasattr(self, 'get') and not hasattr(self, 'head'):
self.head = self.get
self.request = request
self.args = args
self.kwargs = kwargs
return self.dispatch(request, *args, **kwargs)
view.view_class = cls
view.view_initkwargs = initkwargs # take name and docstring from class
update_wrapper(view, cls, updated=()) # and possible attributes set by decorators
# like csrf_exempt from dispatch
update_wrapper(view, cls.dispatch, assigned=())
return view
  # 4.dispatch负责在FBV类中反射相应的方法
  """
  它从request.method中获取请求类型(假设是GET),并进行反射,并交给我们在Class中写好的对应方法(GET)去执行
  那么dispatch就相当于一个请求分发器,它在请求处理前执行
  """
  
def dispatch(self, request, *args, **kwargs):
# Try to dispatch to the right method; if a method doesn't exist,
# defer to the error handler. Also defer to the error handler if the
# request method isn't on the approved list.
if request.method.lower() in self.http_method_names:
# 如果请求方法在self.http_method_not_allowed,就反射相应的方法
handler = getattr(self, request.method.lower(), self.http_method_not_allowed)
else:
# 否则就调用self.http_method_not_allowed
handler = self.http_method_not_allowed
return handler(request, *args, **kwargs) # 执行该方法 def http_method_not_allowed(self, request, *args, **kwargs):
logger.warning(
'Method Not Allowed (%s): %s', request.method, request.path,
extra={'status_code': 405, 'request': request}
)
return HttpResponseNotAllowed(self._allowed_methods()) def options(self, request, *args, **kwargs):
"""Handle responding to requests for the OPTIONS HTTP verb."""
response = HttpResponse()
response['Allow'] = ', '.join(self._allowed_methods())
response['Content-Length'] = ''
return response def _allowed_methods(self):
return [m.upper() for m in self.http_method_names if hasattr(self, m)]
from functools import update_wrapper
class Person:
def __init__(self, **kwargs):
pass @classmethod
def as_View(cls, **initkwargs):
def view(request, *args, **kwargs):
self = cls(**initkwargs)
self.request = request
self.args = args
self.kwargs = kwargs
return self.dispatch(request, *args, **kwargs) view.view_class = cls
view.view_initkwargs = initkwargs
update_wrapper(view, cls, updated=())
update_wrapper(view, cls.dispatch, assigned=()) return view def dispatch(self, request, *args, **kwargs):
print(request)
print(*args)
print(kwargs)
return "HttpHandler" if __name__ == '__main__':
view = Person.as_View(**{"name": "Jan", "age":100})
print(view)
print(view.view_class)
print(view.view_initkwargs)
view("request", 1234, gender="female")
print() """
<function Person at 0x10c75b840>
<class '__main__.Person'>
{'name': 'Jan', 'age': 100}
request
1234
{'gender': 'female'}
"""

as_view的逻辑

二、使用CBV

  对比FBV:

# views.py
from django.shortcuts import render, redirect
from django.views.generic.base import View # CBV写法
class Login(View):
def get(self, request, *args, **kwargs):
return render(request, 'app04/login.html', {"msg": ''}) def post(self, request, *args, **kwargs):
user= request.POST.get("user", False)
pwd = request.POST.get("pwd", False)
if user == "root" and pwd == "root":
request.session["username"] = user
return redirect("index")
else:
msg = "用户名或密码错误"
return render(request, 'app04/login.html', {"msg": ''}) # FBV写法
def login(request):
msg = ""
# print(request.environ["Set-Cookie"])
if request.method == "POST":
user= request.POST.get("user", False)
pwd = request.POST.get("pwd", False)
if user == "root" and pwd == "root":
request.session["username"] = user
return redirect("index")
else:
msg = "用户名或密码错误"
return render(request, 'app04/login.html', {"msg": msg}) def index(request):
username = request.session.get("username")
if username:
return render(request, 'app04/index.html', {'username': username}) return redirect('login')
# urls.py
from django.urls import path
from app04 import views urlpatterns = [
# path('login', views.login, name="login"),
path('index', views.index, name="index"),
path('login', views.Login.as_view(), name="login")
]

三、CBV使用

  django.utils.decorators.py是django内置的装饰器包,提供了对类、方法以及中间件的装饰功能。

"Functions that help with dynamically creating decorators for views."

# For backwards compatibility in Django 2.0.
from contextlib import ContextDecorator # noqa
from functools import WRAPPER_ASSIGNMENTS, update_wrapper, wraps class classonlymethod(classmethod):
def __get__(self, instance, cls=None):
if instance is not None:
raise AttributeError("This method is available only on the class, not on instances.")
return super().__get__(instance, cls) def method_decorator(decorator, name=''):
"""
Convert a function decorator into a method decorator
"""
# 'obj' can be a class or a function. If 'obj' is a function at the time it
# is passed to _dec, it will eventually be a method of the class it is
# defined on. If 'obj' is a class, the 'name' is required to be the name
# of the method that will be decorated.
def _dec(obj):
is_class = isinstance(obj, type)
if is_class:
if name and hasattr(obj, name):
func = getattr(obj, name)
if not callable(func):
raise TypeError(
"Cannot decorate '{0}' as it isn't a callable "
"attribute of {1} ({2})".format(name, obj, func)
)
else:
raise ValueError(
"The keyword argument `name` must be the name of a method "
"of the decorated class: {0}. Got '{1}' instead".format(
obj, name,
)
)
else:
func = obj def decorate(function):
"""
Apply a list/tuple of decorators if decorator is one. Decorator
functions are applied so that the call order is the same as the
order in which they appear in the iterable.
"""
if hasattr(decorator, '__iter__'):
for dec in decorator[::-1]:
function = dec(function)
return function
return decorator(function) def _wrapper(self, *args, **kwargs):
@decorate
def bound_func(*args2, **kwargs2):
return func.__get__(self, type(self))(*args2, **kwargs2)
# bound_func has the signature that 'decorator' expects i.e. no
# 'self' argument, but it is a closure over self so it can call
# 'func' correctly.
return bound_func(*args, **kwargs)
# In case 'decorator' adds attributes to the function it decorates, we
# want to copy those. We don't have access to bound_func in this scope,
# but we can cheat by using it on a dummy function. @decorate
def dummy(*args, **kwargs):
pass
update_wrapper(_wrapper, dummy)
# Need to preserve any existing attributes of 'func', including the name.
update_wrapper(_wrapper, func) if is_class:
setattr(obj, name, _wrapper)
return obj return _wrapper
# Don't worry about making _dec look similar to a list/tuple as it's rather
# meaningless.
if not hasattr(decorator, '__iter__'):
update_wrapper(_dec, decorator)
# Change the name to aid debugging.
if hasattr(decorator, '__name__'):
_dec.__name__ = 'method_decorator(%s)' % decorator.__name__
else:
_dec.__name__ = 'method_decorator(%s)' % decorator.__class__.__name__
return _dec def decorator_from_middleware_with_args(middleware_class):
"""
Like decorator_from_middleware, but return a function
that accepts the arguments to be passed to the middleware_class.
Use like:: cache_page = decorator_from_middleware_with_args(CacheMiddleware)
# ... @cache_page(3600)
def my_view(request):
# ...
"""
return make_middleware_decorator(middleware_class) def decorator_from_middleware(middleware_class):
"""
Given a middleware class (not an instance), return a view decorator. This
lets you use middleware functionality on a per-view basis. The middleware
is created with no params passed.
"""
return make_middleware_decorator(middleware_class)() # Unused, for backwards compatibility in Django 2.0.
def available_attrs(fn):
"""
Return the list of functools-wrappable attributes on a callable.
This was required as a workaround for http://bugs.python.org/issue3445
under Python 2.
"""
return WRAPPER_ASSIGNMENTS def make_middleware_decorator(middleware_class):
def _make_decorator(*m_args, **m_kwargs):
middleware = middleware_class(*m_args, **m_kwargs) def _decorator(view_func):
@wraps(view_func)
def _wrapped_view(request, *args, **kwargs):
if hasattr(middleware, 'process_request'):
result = middleware.process_request(request)
if result is not None:
return result
if hasattr(middleware, 'process_view'):
result = middleware.process_view(request, view_func, args, kwargs)
if result is not None:
return result
try:
response = view_func(request, *args, **kwargs)
except Exception as e:
if hasattr(middleware, 'process_exception'):
result = middleware.process_exception(request, e)
if result is not None:
return result
raise
if hasattr(response, 'render') and callable(response.render):
if hasattr(middleware, 'process_template_response'):
response = middleware.process_template_response(request, response)
# Defer running of process_response until after the template
# has been rendered:
if hasattr(middleware, 'process_response'):
def callback(response):
return middleware.process_response(request, response)
response.add_post_render_callback(callback)
else:
if hasattr(middleware, 'process_response'):
return middleware.process_response(request, response)
return response
return _wrapped_view
return _decorator
return _make_decorator class classproperty:
def __init__(self, method=None):
self.fget = method def __get__(self, instance, cls=None):
return self.fget(cls) def getter(self, method):
self.fget = method
return self

装饰器

  请求处理装饰:

from django.shortcuts import render, redirect
from django.views.generic.base import View
from django.utils.decorators import method_decorator def host(func):
def fn(request, *args, **kwargs):
print(request.get_host())
return func(request, *args, **kwargs)
return fn
def get(func):
def fn(request, *args, **kwargs):
print(request.method)
return func(request, *args, **kwargs)
return fn class Login(View):
@method_decorator(host) # 给dispatch添加装饰器,所有请求方式都执行
def dispatch(self, request, *args, **kwargs):
return super().dispatch(request, *args, **kwargs) # dispatch反射,一定要return @method_decorator(get) # 给单个请求添加装饰器
def get(self, request, *args, **kwargs):
return render(request, 'app04/login.html', {"msg": ''}) def post(self, request, *args, **kwargs):
user= request.POST.get("user", False)
pwd = request.POST.get("pwd", False)
if user == "root" and pwd == "root":
request.session["username"] = user
return redirect("index")
else:
msg = "用户名或密码错误"
return render(request, 'app04/login.html', {"msg": ''})

  上面可以重写,把装饰器写在类上,并指定给要装饰的函数

from django.shortcuts import render, redirect
from django.views.generic.base import View
from django.utils.decorators import method_decorator def host(func):
def fn(request, *args, **kwargs):
print(request.get_host())
return func(request, *args, **kwargs)
return fn
def get(func):
def fn(request, *args, **kwargs):
print(request.method)
return func(request, *args, **kwargs)
return fn @method_decorator(get, name="get") # 给单个请求添加装饰器
@method_decorator(host, name="dispatch") # 给dispatch添加装饰器,所有请求方式都执行;一定要通过name指定要装饰的函数
class Login(View): def dispatch(self, request, *args, **kwargs):
return super().dispatch(request, *args, **kwargs) # dispatch反射,一定要return
def get(self, request, *args, **kwargs):
return render(request, 'app04/login.html', {"msg": ''})
def post(self, request, *args, **kwargs):
user= request.POST.get("user", False)
pwd = request.POST.get("pwd", False)
if user == "root" and pwd == "root":
request.session["username"] = user
return redirect("index")
else:
msg = "用户名或密码错误"
return render(request, 'app04/login.html', {"msg": ''})

四、view类及其子类

  view相关类在django.views.generic文件下。它通过__all__指明了所有可以调用的视图函数及其子类。其中的"View"位于.base.py文件里,是其它类的基类,它的相关内容已经在上文做了标注。其它子类用于辅助快速构建视图函数。

# django.views.generic.__init__.py
from django.views.generic.base import RedirectView, TemplateView, View
from django.views.generic.dates import (
ArchiveIndexView, DateDetailView, DayArchiveView, MonthArchiveView,
TodayArchiveView, WeekArchiveView, YearArchiveView,
)
from django.views.generic.detail import DetailView
from django.views.generic.edit import (
CreateView, DeleteView, FormView, UpdateView,
)
from django.views.generic.list import ListView __all__ = [
'View', 'TemplateView', 'RedirectView', 'ArchiveIndexView',
'YearArchiveView', 'MonthArchiveView', 'WeekArchiveView', 'DayArchiveView',
'TodayArchiveView', 'DateDetailView', 'DetailView', 'FormView',
'CreateView', 'UpdateView', 'DeleteView', 'ListView', 'GenericViewError',
] class GenericViewError(Exception):
"""A problem in a generic view."""
pass

  1.TemplateView

  用于在get请求时直接返回一个静态模板。源码如下:

class TemplateView(TemplateResponseMixin, ContextMixin, View):   # 只支持get
"""
Render a template. Pass keyword arguments from the URLconf to the context.
"""
def get(self, request, *args, **kwargs):
context = self.get_context_data(**kwargs)
return self.render_to_response(context)

  用例:

# app01/urls.py
from django.urls import path
from django.views.generic import TemplateView
urlpatterns = [
path("example1", TemplateView.as_view(template_name="app01/example1.html")), # 不用在app01/views.py中写视图函数
]
# templates/app01/example1.html
...
<h1>this is a example1</h1>
...

  2.ListView

  给静态文件传递queryset列表。源码如下:

class ListView(MultipleObjectTemplateResponseMixin, BaseListView):   # 继承了这两个父类
"""
Render some list of objects, set by `self.model` or `self.queryset`.
`self.queryset` can actually be any iterable of items, not just a queryset.
"""

  用例:

# app01/urls.py
from django.urls import path
from .models import Book
from django.views.generic import *
bookinfo = {
'queryset': Book.objects.all(), # Book.objects.all去掉括号,会动态地进行查询
'template_name': "app01/example2.html"
} urlpatterns = [
path("example1", TemplateView.as_view(template_name="app01/example1.html")),
path("example2", ListView.as_view(**bookinfo)),
] # templates/app01/example2.py
...
<p>example2</p>
{% for obj in object_list %} # 注意这里的object_list,它是默认的变量名,即对应bookinfo里的queryset.
<p>{{ obj.id }}, {{ obj.name }}</p>
{% endfor %}
...

  它其实等价于:

# app01/urls.py

from django.urls import path
from .views import *
urlpatterns = [
path("example3", BookListView.as_view(),)
] # app01/views.py
from django.views.generic import ListView
from .models import Book
class BookListView(ListView):
model = Book
queryset = Book.objects.all()
template_name = "app01/example2.html" def get_context_data(self, *, object_list=None, **kwargs):
return super().get_context_data(**kwargs)

django(六):view和cbv的更多相关文章

  1. Django的View(视图)和路由系统

    一.Django的View(视图) 1.介绍 一个视图函数(类),简称视图,是一个简单的Python 函数(类),它接受Web请求并且返回Web响应. 响应可以是一张网页的HTML内容,一个重定向,一 ...

  2. Django的View(视图层)

    目录 Django的View(视图层) 一.JsonResponse 二.后端接收前端的文件 三. FBV和CBV(源码分析) 四.settings.py配置文件源码分析 五. 请求对象(HttpRe ...

  3. django的FBV和CBV

    title: python djano CBV FBV tags: python, djano, CBV, FBV grammar_cjkRuby: true --- python django的fu ...

  4. Django的View(视图)

    Django的View(视图) 一个视图函数(类),简称视图,是一个简单的Python 函数(类),它接受Web请求并且返回Web响应. 响应可以是一张网页的HTML内容,一个重定向,一个404错误, ...

  5. 通过django的rest-framework……(CBV)

    为什么不使用FBV,因为CBV重用性很高 先看一个例子: from django.views.generic.base import View from django.http import Http ...

  6. WEB框架Django之中间件/缓存/CBV/信号

    一Djano的中间件 1 中间件的概念 中间件顾名思义,是介于request与respose处理之间的一道处理过程,相对比较轻量级,并且全局上改变django的输入与输出.因为改变是全局, 所有需要谨 ...

  7. Python学习---django知识补充之CBV

    Django知识补充之CBV Django: url    -->  def函数      FBV[function based view]  用函数和URL进行匹配 url    --> ...

  8. django中介模型,CBV模型,及logging日志配制

    1.中介模型 中介模型,这个是在我们创建表格时,多对多添加的时候应用到的,通过制定ManyToManyField字段中的through参数来定义,为两者的关系新建一个中介class 为什么会产生这个中 ...

  9. day 67 Django的view 与路由

    一.Django中的视图 CBV和FBV 我们之前写过的都是基于函数的view,就叫FBV.还可以把view写成基于类的. url(r'^add_publisher/',views.AddPublis ...

  10. Django 反向解析 request CBV

    正则路径中的分组 无名分组 分组的概念:就是给某一段正则表达式用小括号括起来 无名分组按位置传参数,一一对应. view中除去request,其他形参数量要与urls中分组数量一致. 无名分组就是将括 ...

随机推荐

  1. 洛谷P3369 【模板】普通平衡树(Splay)

    题面 传送门 题解 鉴于最近的码力实在是弱到了一个境界--回来重新打一下Splay的板子--竟然整整调了一个上午-- //minamoto #include<bits/stdc++.h> ...

  2. Python 将一个已知的 utc时间字符串 转换为东八区时间

    先获取一个utc格式的时间 utc_time = datetime.datetime.utcnow() print(utc_time) 输出 2018-06-24T08:59:39Z 这里我们假设目前 ...

  3. [As3.0] 获取本机信息

    package { import flash.display.Sprite; import flash.events.Event; import flash.net.NetworkInfo; impo ...

  4. vue项目打包后路径出错

    安装完vue后搭建了一个项目,直接执行 npm run dev 是可以正常打开页面的: 但是执行 npm run build 打包项目后打开却报错了,如下: 原来是项目中的静态文件路径报错了... 然 ...

  5. js之作用域

    1.什么是作用域 作用域是用于收集存储维护变量,以及当前执行代码声明的变量所拥有的权限, 例如 : function foo(a){ console.log(a); --------    1    ...

  6. KBEngine 安装

    其实这篇的内容官方文档都有, 但是既然打算记录一下学习笔记, 也就先从安装开始了. 一 下载源代码 进入github下载最新release的源码压缩包. windows选择zip, 下载完成之后右键解 ...

  7. mapreduce基本原理

    场景: 一个大小为100T的文件,统计单词"ERROR"和"INFO"的个数 普通做法 是不是效率太低了? 换个方式 说明: 把100T文件分成100份,一台机 ...

  8. Tomcat性能调优-让小猫飞奔

    一.总结前一天的学习 从“第三天”的性能测试一节中,我们得知了决定性能测试的几个重要指标,它们是: ü   吞吐量 ü   Responsetime ü   Cpuload ü   MemoryUsa ...

  9. Win7 Eclipse调试Centos Hadoop2.2-Mapreduce(转)

    一. 自己搭建开发环境 今天自己搭建了一套Centos5.3 + Hadoop2.2 + Hbase0.96.1.1的开发环境,Win7 Eclipse调试MapReduce成功.可能是版本比较高的原 ...

  10. 解析ASP.NET Mvc开发之查询数据实例 分类: ASP.NET 2014-01-02 01:27 5788人阅读 评论(3) 收藏

    目录: 1)从明源动力到创新工场这一路走来 2)解析ASP.NET WebForm和Mvc开发的区别 ----------------------------------------------- ...