本文共 1647 字,大约阅读时间需要 5 分钟。
def func_view(request): "Function views" if request.method == "GET": return HttpResponse("GET-请求") elif request.method == "POST": return HttpResponse('POST-请求') elif request.method == "PUT": return HttpResponse('PUT-请求') elif request.method == "DELETE": return HttpResponse('DELETE-请求') else: return HttpResponse("不支持的请求类型")
class MyClassView(View): def get(self, request, *args, **kwargs): return HttpResponse("GET-请求") def post(self, request, *args, **kwargs): return HttpResponse("POST-请求") def put(self, request, *args, **kwargs): return HttpResponse("PUT-请求") def delete(self, request, *args, **kwargs): return HttpResponse("DELETE-请求")
结论: CBV基于反射实现根据请求方式不同,执行不同方法
原理总结:
路由url -- > view函数 --> dispatch方法(根据反射来执行:GET/POST/PUT/DELETE等等....)转载地址:http://ouaga.baihongyu.com/