标签:接下来 应该 用户 @class strip html 继承 多态 django
url:
url(r‘register/$‘,views.Register.as_view())
django的CBV在url中的书写需要在view.类名后加.as_view(),从中可以了解到as_view一定是一个方法,这个方法加上了(),是在Django项目启动时就自动执行的,返回值也应该是 一个函数名,从而在匹配url后可以执行相应的函数
PS:关于分析源码的首要任务是需要清楚方法的调用者和self的归属类!!!很重要
view.py:
class Register(View):
def get(self,request):
return render(request, ‘register.html‘)
源码:
@classonlymethod
def as_view(cls, **initkwargs):
pass
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)
return view
源码执行简易流程图:
到此可以很明显的看出,Django项目启动后,CBV下的url执行后相当于:
url(r‘register/$‘,views.Register.view)
源码:以下源码注意两点 1. cls涉及到了----《闭包作用域 》; 2. self的指向
@classonlymethod
def as_view(cls, **initkwargs):
pass
def view(request, *args, **kwargs):
self = cls(**initkwargs) //cls是Register类,self是实例化的对象
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)
return view
view.py: 找dispatch方法时需要先找自己类中,没有再找继承类中
class Register(View):
def get(self,request):
return render(request, ‘register.html‘))
源码: 执行对应的dispatch方法
def dispatch(self, request, *args, **kwargs):
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)
此文章来自于https://www.jianshu.com/p/9e4e3195d731
标签:接下来 应该 用户 @class strip html 继承 多态 django
原文地址:https://www.cnblogs.com/YZL2333/p/11684735.html