day93 CBV与FBV
1.什么是FBV?
全称:function base view
基于函数的视图函数
from django.http import JsonResponse def index(request): if request.method == 'POST': // 获取前端用户输入的数据 name = request.POST.get('name') age = request.POST.get('age') //修改数据库 return JsonResponse('OK') return render(request,'index.html')
2.什么是CBV?
全称:class base view
基于类的视图函数
from django import views class Index(views.View): def get(self,request): return render(request,'index.html') def post(self,request): return HttpResponse('OK')
url中的路径配置: path('index2',views.index.as_view())
as_view()
picture1:
picture2:
picutre3:
注意事项:
1.CBV定义一定要继承django.views.View
2.注册路由的时候要写类名.as_view()
3.具体原理是:dispatch()方法中利用反射,找到每个请求要执行的方法