Django Class View Comments

View type is introduced
       when the view is defined in the form of the function is a function of view, the view function to facilitate the understanding, but encountered a path corresponding to the view function provides support for a variety of different ways HTTP request (get, post, delete, put ) , you need to write a function in different business logic, readability and reusability of code is very low end, so, we introduce the class view to resolve.
The following is an example of a function of view

[Python]  plain text view  Copy the code
?
01
02
03
04
05
06
07
08
09
10
def register(request):
     """处理注册"""
  
     # 获取请求方法,判断是GET/POST请求
     if request.method = = 'GET' :
         # 处理GET请求,返回注册页面
         return render(request, 'register.html' )
     else :
         # 处理POST请求,实现注册逻辑
         return HttpResponse( '这里实现注册逻辑' )


Class view of the advantages
       the code readability good
       class view relative to the view function has a higher reusability, if other places need to use a specific method of a class, the direct successor of the class will be able to view
class view defined
in the class which define different views according to different request handler manner
define classes view need to inherit from the parent class view provided by Django

[Python]  plain text view  Copy the code
?
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
from django.views.generic import View
 
 
class DefineClassview(View):
     """演示类视图的定义和使用"""
 
     def get( self , request):
         """处理GET请求业务逻辑"""
         return HttpResponse( 'GET请求业务逻辑' )
 
     def post( self , request):
         """处理POST请求业务逻辑"""
         return HttpResponse( 'POST请求业务逻辑' )
 
     def put( self , request):
         """处理PUT请求业务逻辑"""
         return HttpResponse( 'PUT请求业务逻辑' )
 
     def delete( self , request):
         """处理DELETE请求业务逻辑"""
         return HttpResponse( 'DELETE请求业务逻辑' )


Configuring url: as_view call the class view () function
Note: as_view () function is inherited from the parent class View

[Python]  plain text view  Copy the code
?
1
2
3
4
5
6
urlpatterns = [
     # 视图函数:注册
     # url(r'^register/$', views.register, name='register'),
     # 类视图:注册  as_view()可以将类视图转换成视图,并决定如何分发请求
     url(r '^register/$' , views.RegisterView.as_view(), name = 'register' ),
]


The principle Class View
from the following source code, we can see as_view () function is defined inside a view () function and returns view.
We look at the view () function, which calls the dispatch () function, dispatch () function returns a call request method different ways according to the different requests.

[Python]  plain text view  Copy the code
?
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
@classonlymethod
def as_view( cls , * * initkwargs):
     """
     Main entry point for a request-response process.
     """
     ...省略代码...
 
     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
         # 调用dispatch方法,按照不同请求方式调用不同请求方法
         return self .dispatch(request, * args, * * kwargs)
 
     ...省略代码...
 
     # 返回真正的函数视图
     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)

 

 

More technical information may concern: gzitcast

Guess you like

Origin www.cnblogs.com/heimaguangzhou/p/11689916.html