用类的方式进行cbv(Class-Based-View)模式 这里也是django框架的扩展点之一

   urls  

url(r'^cbv$', views.CBV.as_view()),#类的处理方式,在django中后面的要加



views
from django.views import View
class CBV(View):

# 这个方法的作用在于可以把get和post方法相同的地方写在dispatch中
def dispatch(self, request, *args, **kwargs):#调用父类的dispatch方法
print('dispatch....')
result=super(CBV,self).dispatch(request, *args, **kwargs)
return result#这里是接收dispatch的值
# http_method_names = ['get', 'post', 'put', 'patch', 'delete', 'head', 'options', 'trace']
  #form表单只能get和post,但是ajax支持所有方式

# 请求发来后执行dispatch方法(反射),然后再找时get还是post
def get(self,request):#如果以get类发的请求会自动触发CBV类中的get方法
# 返回路径先返回到dispatch然后返回到用户
# 找的方式,请求头中看是post方法还是post方法
# cbv怎么执行的方法,根据请求头中的request.method进行自动执行
return render(request,'cbv.html')
# return HttpResponse('CBV.GET')

def post(self,request):#返回路径先dispatch然后再返回到用户
ret = HttpResponse('CBV.POST')
ret['h1']='v1'#添加一个响应头在响应体中存放内容
ret.set_cookie('c1','v2')#服务端写cookie
'''
头: h1=v1
cookies:c1=v2

体:CBV.POST
'''
return ret
 
posted on 2019-07-24 14:36  帅的遭人砍see  阅读(141)  评论(0编辑  收藏  举报