django(六):view和cbv
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的更多相关文章
- Django的View(视图)和路由系统
一.Django的View(视图) 1.介绍 一个视图函数(类),简称视图,是一个简单的Python 函数(类),它接受Web请求并且返回Web响应. 响应可以是一张网页的HTML内容,一个重定向,一 ...
- Django的View(视图层)
目录 Django的View(视图层) 一.JsonResponse 二.后端接收前端的文件 三. FBV和CBV(源码分析) 四.settings.py配置文件源码分析 五. 请求对象(HttpRe ...
- django的FBV和CBV
title: python djano CBV FBV tags: python, djano, CBV, FBV grammar_cjkRuby: true --- python django的fu ...
- Django的View(视图)
Django的View(视图) 一个视图函数(类),简称视图,是一个简单的Python 函数(类),它接受Web请求并且返回Web响应. 响应可以是一张网页的HTML内容,一个重定向,一个404错误, ...
- 通过django的rest-framework……(CBV)
为什么不使用FBV,因为CBV重用性很高 先看一个例子: from django.views.generic.base import View from django.http import Http ...
- WEB框架Django之中间件/缓存/CBV/信号
一Djano的中间件 1 中间件的概念 中间件顾名思义,是介于request与respose处理之间的一道处理过程,相对比较轻量级,并且全局上改变django的输入与输出.因为改变是全局, 所有需要谨 ...
- Python学习---django知识补充之CBV
Django知识补充之CBV Django: url --> def函数 FBV[function based view] 用函数和URL进行匹配 url --> ...
- django中介模型,CBV模型,及logging日志配制
1.中介模型 中介模型,这个是在我们创建表格时,多对多添加的时候应用到的,通过制定ManyToManyField字段中的through参数来定义,为两者的关系新建一个中介class 为什么会产生这个中 ...
- day 67 Django的view 与路由
一.Django中的视图 CBV和FBV 我们之前写过的都是基于函数的view,就叫FBV.还可以把view写成基于类的. url(r'^add_publisher/',views.AddPublis ...
- Django 反向解析 request CBV
正则路径中的分组 无名分组 分组的概念:就是给某一段正则表达式用小括号括起来 无名分组按位置传参数,一一对应. view中除去request,其他形参数量要与urls中分组数量一致. 无名分组就是将括 ...
随机推荐
- s11 day 101 python Linux环境安装 与路飞项目支付功能
from django.conf.urls import urlfrom django.contrib import adminfrom app01 import viewsurlpatterns = ...
- 使用dev-tool定位页面性能瓶颈
这是部门同事的一次内部分享,听完后受益颇多,趁着记忆还算新鲜,赶紧记录一波. 从 dev-tool 看页面 parse 过程 时间都去哪儿了 当浏览器发送一个请求到接受所有响应数据截止,这个过程发生了 ...
- Python中的运算符与表达式
你所编写的大多数语句(逻辑行)都包含了表达式(Expressions).一个表达式的简单例子便是 2+3.表达式可以拆分成运算符(Operators)与操作数(Operands).运算符(Operat ...
- 客户端集成IdentityServer4
1. vs code 终端执行 dotnet new webapi --name ClientCredentialApi 2. 找到ValuesController.cs 引用 using Mic ...
- jvm高级特性(4)(内存分配回收策略)
JVM高级特性与实践(四):内存分配 与 回收策略 一. 内存分配 和 回收策略 1,对象内存分配的概念: 往大方向讲,它就是在堆上分配(但也可能经过JIT编译后被拆散为标量类型并间接地栈上分配), ...
- php 词法分析,语法分析
php的词法分析 可以理解为 通过一定的规则,把输入的代码 区分出哪些是 是$开头的变量, 哪些是 以两个单引号括起来的字符串,哪些是以两个双引号括起来的字符串 等等, 这些区分出来的东西 称为tok ...
- POJ 2301
#include<iostream> #include<stdio.h> using namespace std; int main() { //freopen("a ...
- logcat -- 基本用法
1.Log类是一个日志类,我们可以在代码中使用logcat打印出消息 常见的日志记录方法有: v(String,String) --verbose 显示全部信息 d(String,String) - ...
- 剑指offer五十六之删除链表中重复的结点
一.题目 在一个排序的链表中,存在重复的结点,请删除该链表中重复的结点,重复的结点不保留,返回链表头指针. 例如,链表1->2->3->3->4->4->5 处理后 ...
- Java之集合(八)HashMap
转载请注明源出处:http://www.cnblogs.com/lighten/p/7338372.html 1.前言 本章介绍Java中最常用的一个集合类HashMap,此类在不同的JDK版本有不同 ...