Django View类的解析

class View(object):
    """
    Intentionally simple parent class for all views. Only implements
    dispatch-by-method and simple sanity checking.
    """

    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 six.iteritems(kwargs):
            setattr(self, key, value)

    @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))

        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
            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

    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:
            handler = getattr(self, request.method.lower(), self.http_method_not_allowed)
        else:
            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 http.HttpResponseNotAllowed(self._allowed_methods())


View类的部分源码如上所示。那么平时使用 类 类型的视图是如何解析的呢,下面进行简单介绍:

在url中通常有如下代码

------------------------------------------------------------------------------------------------------------------------------------------------------------->


urlpatterns=[

    url(r'^$',SomeView.as_view(),name='index'),

    url(r'^about/$',AboutView.as_view(),name='about'),

]

------------------------------------------------------------------------------------------------------------------------------------------------------------->

当有人访问我们的网站根目录的时候,url会在url列表中进行匹配,找到第一个匹配项则把对应的request和其他参数传递给对应的视图

那么在上面的urlpatterns中则一定会匹配到第一条,也就是会把request等信息传递给SomeView.as_view()方法。因为SomeView继承自View,而我们没有去实现as_view()方法

,那么也就是View.as_view()会进行处理。

而处理的过程如下,解释标注在下面代码中:

 @classonlymethod

    def as_view(cls, **initkwargs):
        """
        Main entry point for a request-response process.
        """
        for key in initkwargs: #首先是判断 在urlpatterns中as_view有没有传入 http方法的参数,比如我传入一个 get='post',
            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): #如果传入的参数并不存在于 视图类中,比如我传入 temp='123',而我的视图类中并没有该属性,也会抛出异常
                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'):#如果类视图实现了 get方法,并且没实现head方法,则head方法等同于get方法
                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 #把最终的视图处理返回用于调用

------------------------------------------------------------------------------------------------------------------------------------------------------------->

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:#判断request中的方法是不是类视图允许的方法 
                                                         #如果是允许的方法,则会在对应的类视图中的方法传给给handler用于之后的处理
                                                        #如果是允许的方法,但是类视图中没有实现该方法,则把http_method_not_allowed方法返回
            handler = getattr(self, request.method.lower(), self.http_method_not_allowed)
        else:#是不允许的方法,则返回 http_method_not_allowed方法,源码在开头
            handler = self.http_method_not_allowed
        return handler(request, *args, **kwargs)

欢迎大家批评指正

转载于:https://www.cnblogs.com/hiveme/p/8194830.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值