from django.views import View
class 类名(View):
def method(request):
return template
def view(cls,request,*args,**kwargs):
self = cls(*args,**kwargs)
#cls是谁调用就是谁
return self.dispatch(request,*args,**kwargs)
def dispatch(self,request,*args,**kwargs):
if(request.method.lower() in self.http_method_names):
hander = getattr(self,request,request.method.lower(),self.http_method_not_allowed)
else:
hander = self.http_method_not_allowed #错误页面
return hander(request,*args,**kwargs)
var = getattr(对象,属性字符串 | 函数字符串,找不到时调用它)
#等价于:对象.属性 | 函数
super().父类属性
作用是为类传值
def __init__(self,var,....):
self.var = var
...
constructor(var,...){
thsi.var = var
}
APIView继承了View(django中的view),但重构了dispatch函数
APIView(view)—>as_view()—>view—>dispatch
重构了dispatch函数,做了三件事,
request = initialize_request(self,*args,**kwargs)
self.request = request
self.initail(request,*args,**kwargs)#包含认证、权限、限流
在djagno中contentType
但重构后的request.data两种都能获取
在django获取url路由中的值是用request.GET,但在重构的request中用request.query_params获取
使用request._request获取原来的request