视图

视图

两种模式: 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)

posted @ 2021-03-09 20:52  你也想起舞吗ovo  阅读(38)  评论(0)    收藏  举报