视图
视图
两种模式: FBV和CBV
视图函数 FBV
def index(request):pass
视图类 CBV
from django.views import View
class IndexVIew(View):
def get(self,request):pass
路由:
url(r'^index/',views.IndexView.as_view());
反射: 通过请求方法来反射获取对应的类方法执行
def dispatch(self, request, *args, **kwargs):
# Try to dispatch to the right method; if a method doesn't exist,
# defer to the error handler. Also defer to the error handler if the
# request method isn't on the approved list.
# GET--get in http_method_names = ['get', 'post', 'put', 'patch', 'delete', 'head', 'options', 'trace']
if request.method.lower() in self.http_method_names:
handler = getattr(self, request.method.lower(), self.http_method_not_allowed)
else:
handler = self.http_method_not_allowed
return handler(request, *args, **kwargs)

浙公网安备 33010602011771号