Introduction: There are two views Django treatment:
- FBV (function base views) in the view is based on processing request function form.
- CBV (class base views) is based on processing request as a class in the view.
Python is an object-oriented programming language, if only a function to develop, there are many advantages of object-oriented missing out (inheritance, encapsulation, polymorphism). So Django later joined the Class-Based-View. It allows us to use the class to write View. The advantage of this is mainly the following two:
- Improve the reusability of code, object-oriented techniques may be used, such as Mixin (multiple inheritance)
- May be processed for various different functions HTTP method, in many if not determining, improve code readability
Here we look at that django source, so as to further understanding of Django CBV
A first look url:
url:
url(r'register/$',views.Register.as_view())
django of CBV writing in the url needs in view. After the class name plus .as_view (), which you can learn as_view must be a method that added (), it is automatically executed when Django project started, the return value should also be a function name, the corresponding function can be performed after matching url
PS: the primary task on the analysis of the source code is the need to understand the caller and the method of self ownership class! ! ! It is important
view.py:
class Register(View): def get(self,request): return render(request, 'register.html')
Source:
@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
Source of the easy flow chart:
This can clearly be seen, after Django project started after the execution url CBV at the equivalent of:
url(r'register/$',views.Register.view)
Second, when a user accesses the next url, url corresponding to the view class the method is executed in a manner corresponding to the request
Source: The following two points source 1. cls ---- relates to "closure scope"; 2. self point of
@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: the need to go first to their own class, did not find the time to find a derived class method dispatch
class Register(View): def get(self,request): return render(request, 'register.html'))
Source: executes a corresponding dispatch method
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)
Django source CBV this execution process based on the end, a good use of object-oriented python three characteristics: inheritance, polymorphism and encapsulation.
此文章来自于https://www.jianshu.com/p/9e4e3195d731