视图

FBV:在视图中用函数

CBV:在哪视图中用类

CBV:

  定义: from django.views import view

     放到类的参数内  如:class AddPublisher(View):

  使用: url(r'^add_publisher/', views.AddPublisher.as_view()),

给CBV加装饰器:

  1,from django.utils.decorators import method_decorator

    加载某个get/post的方法上:@method_decorator(timer)
                 def get(self, request):

    加在self.dispatch方法上:@method_decorator(timer)
                def dispatch(self, request, *args, **kwargs):

    加在类上:     @method_decorator(timer, name='post')
              @method_decorator(timer, name='get')
              class AddPublisher(View):

request

 1 常用属性:
 2 
 3 
 4 print(request.method)   # GET 请求方式
 5 print(request.GET)      # {}  []  get()     包含所有HTTP  GET参数的类字典对象
 6 print(request.POST)      # {}  []  get()    包含所有HTTP POST参数的类字典对象
 7 print(request.FILES)         #上传信息
 8 print(request.path_info,type(request.path_info))   # 路径信息   不包含IP和端口、参数
 9 常用方法:
10 print(request.get_host()) #IP 端口
11 print(request.get_full_path()) # 路径信息 + 参数 
原文地址:https://www.cnblogs.com/leo-tail-x/p/10067562.html