django CBV源码

django CBV源码,第1张

django CBV源码

FBV(function base views) 在视图里使用函数处理请求

CBV(class base views) 在视图里使用类处理请求。

CBV源码解析
from django.urls import path
from django.views import View
from django.shortcuts import HttpResponse

class Books(View):
    def get(self, request):
        return HttpResponse('Book')

urlpatterns = [
    path('book', Books.as_view())
]

我们可以通过Books.as_view()来设置成路由函数,这个过程是如何转变的。

http_method_names = ['get', 'post', 'put', 'patch', 'delete', 'head', 'options', 'trace']
@classonlymethod
def as_view(cls, **initkwargs):
    for key in initkwargs:	# 检查传递进来的参数
        if key in cls.http_method_names:	# key 不能为 http_method_names 里面的参数
            raise TypeError(
                'The method name %s is not accepted as a keyword argument '
                'to %s().' % (key, cls.__name__)
            )
        if not hasattr(cls, key): # key 不能是 cls 没有的参数
            raise TypeError("%s() received an invalid keyword %r. as_view "
                            "only accepts arguments that are already "
                            "attributes of the class." % (cls.__name__, key))

    def 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
        
        if not hasattr(self, 'request'):
            raise AttributeError(
                "%s instance has no 'request' attribute. Did you override "
                "setup() and forget to call super()?" % cls.__name__
            )
        return self.dispatch(request, *args, **kwargs)
    
  	view.view_class = cls	# 将 cls 挂载到 view 上面
    view.view_initkwargs = initkwargs	# 将 initkwargs 挂载到 view 上面
    update_wrapper(view, cls, updated=()) 
    update_wrapper(view, cls.dispatch, assigned=()) 
    return view


def dispatch(self, request, *args, **kwargs):
    if request.method.lower() in self.http_method_names:	# 通过 request 里面的请求方式绑定函数 如果 cls 里面没有就会报错
        handler = getattr(self, request.method.lower(), self.http_method_not_allowed)
    else:
        handler = self.http_method_not_allowed
        return handler(request, *args, **kwargs)
  1. 参数与对象元信息绑定在 view 函数上面
    1. 通过 functools.update_wrapper 将元信息加载到view函数里面
  2. 根据请求方式返回元信息里面写好的请求函数,如果没有就会报错
    1. 通过 self.dispatch 将返回相应的请求方式函数

注意

  • request是网页响应参数由每个客户端发送过来的,是路由函数必须有的参数。
  • 通过控制http_method_names的值限制请求方式。
 @classmethod
 def as_view(cls, **initkwargs):
    """
        Store the original class on the view function.

        This allows us to discover information about the view when we do URL
        reverse lookups.  Used for breadcrumb generation.
        """
    if isinstance(getattr(cls, 'queryset', None), models.query.QuerySet):
        def force_evaluation():
            raise RuntimeError(
                'Do not evaluate the `.queryset` attribute directly, '
                'as the result will be cached and reused between requests. '
                'Use `.all()` or call `.get_queryset()` instead.'
            )
        cls.queryset._fetch_all = force_evaluation

    view = super().as_view(**initkwargs)
    view.cls = cls
    view.initkwargs = initkwargs
    
    return csrf_exempt(view)


def csrf_exempt(view_func):
    def wrapped_view(*args, **kwargs):
        return view_func(*args, **kwargs)
    wrapped_view.csrf_exempt = True
    return wraps(view_func)(wrapped_view)

functools之update_wrapper的使用 - 云+社区 - 腾讯云 (tencent.com)

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5437013.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-11
下一篇 2022-12-11

发表评论

登录后才能评论

评论列表(0条)

保存