django 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()会进行处理。

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

[python] view plain copy
  1. @classonlymethod  
  2.   
  3.    def as_view(cls, **initkwargs):  
  4.        """ 
  5.        Main entry point for a request-response process. 
  6.        """  
  7.        for key in initkwargs: #首先是判断 在urlpatterns中as_view有没有传入 http方法的参数,比如我传入一个 get='post',  
  8.            if key in cls.http_method_names: #如果传入了呢,则会抛出异常,  
  9.                raise TypeError("You tried to pass in the %s method name as a "  
  10.                                "keyword argument to %s(). Don't do that."  
  11.                                % (key, cls.__name__))  
  12.            if not hasattr(cls, key): #如果传入的参数并不存在于 视图类中,比如我传入 temp='123',而我的视图类中并没有该属性,也会抛出异常  
  13.                raise TypeError("%s() received an invalid keyword %r. as_view "  
  14.                                "only accepts arguments that are already "  
  15.                                "attributes of the class." % (cls.__name__, key))  
  16.   
  17.        def view(request, *args, **kwargs): #此方法主要用于返回请求的 视图  
  18.            self = cls(**initkwargs)   
  19.            if hasattr(self'get'and not hasattr(self'head'):#如果类视图实现了 get方法,并且没实现head方法,则head方法等同于get方法  
  20.                self.head = self.get  
  21.            self.request = request  
  22.            self.args = args  
  23.            self.kwargs = kwargs  
  24.            return self.dispatch(request, *args, **kwargs) #这是查找类视图中对应方法的重要步骤  
  25.        view.view_class = cls  
  26.        view.view_initkwargs = initkwargs  
  27.   
  28.        # take name and docstring from class  
  29.        update_wrapper(view, cls, updated=())  
  30.   
  31.        # and possible attributes set by decorators  
  32.        # like csrf_exempt from dispatch  
  33.        update_wrapper(view, cls.dispatch, assigned=())  
  34.        return view #把最终的视图处理返回用于调用  

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

dispatch解释标注在下面代码中

[python] view plain copy
  1.     def dispatch(self, request, *args, **kwargs):  
  2.         # Try to dispatch to the right method; if a method doesn't exist,  
  3.         # defer to the error handler. Also defer to the error handler if the  
  4.         # request method isn't on the approved list.  
  5.         if request.method.lower() in self.http_method_names:#判断request中的方法是不是类视图允许的方法   
  6.                                                          #如果是允许的方法,则会在对应的类视图中的方法传给给handler用于之后的处理  
  7.                                                         #如果是允许的方法,但是类视图中没有实现该方法,则把http_method_not_allowed方法返回  
  8.             handler = getattr(self, request.method.lower(), self.http_method_not_allowed)  
  9.         else:#是不允许的方法,则返回 http_method_not_allowed方法,源码在开头  
  10.             handler = self.http_method_not_allowed  
  11.         return handler(request, *args, **kwargs
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值