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)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

hjhcos

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值