CourseModuleUpdateView未返回HttpResponse对象。它返回了None

时间:2017-11-04 16:28:45

标签: python django

我有以下课程,用于使用formsets显示相关课程模块

class CourseModuleUpdateView(TemplateResponseMixin, View):
    template_name = 'courses/manage/module/formset.html'
    course = None

    def get_formset(self, data=None):
        return ModuleFormSet(instance=self.course, data=data)

    def dispatch(self, request, *args, **kwargs):
        self.course = get_object_or_404(Course,
                                        id=kwargs['pk'],
                                        owner=request.user)
        super(CourseModuleUpdateView, self).dispatch(request, *args, **kwargs)

    def get(self, request, *args, **kwargs):
        formset = self.get_formset()
        return self.render_to_response({'course': self.course, 'formset': formset})

负责此CBV的Url模式

url(r'^(?P<pk>\d+)/module/$', views.CourseModuleUpdateView.as_view(), name='course_mudule_update')

发出get请求我收到以下错误

回溯:

File "/home/mtali/.virtualenvs/educa/lib/python3.5/site-packages/django/core/handlers/exception.py" in inner
  41.             response = get_response(request)

File "/home/mtali/.virtualenvs/educa/lib/python3.5/site-packages/django/core/handlers/base.py" in _get_response
  198.                 "returned None instead." % (callback.__module__, view_name)

Exception Type: ValueError at /courses/4/module/ Exception Value: The view courses.views.CourseModuleUpdateView didn't return an HttpResponse object. It returned None instead.

我的代码出了什么问题!我正在使用django 1.11

1 个答案:

答案 0 :(得分:0)

按照Django documentation调度方法,应该返回HTTP响应。

  
    

dispatch(request,* args,** kwargs)¶

         
      

视图的视图部分–       接受请求参数和参数的方法,返回一个       HTTP响应。

    
  

基于您的代码的说明。

从Django 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.
        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)

dispatch方法不仅将请求的方法委托给相应的处理程序,而且还<< strong>返回处理程序响应。

即在此行

return handler(request, *args, **kwargs)

在您的情况下,您正在super(CourseModuleUpdateView, self).dispatch(request, *args, **kwargs)方法中调用dispatch,这将调用超类的调度方法(即View类调度方法)。由于您在GET方法上执行了以下一行后,您请求的http方法是dispatch

handler = getattr(self, request.method.lower(), self.http_method_not_allowed)

处理程序将分配给get。因此,在这种情况下,它大致等于

def dispatch(self, request, *args, **kwargs):
    return get(request, *args, **kwargs)

get将在您的get类中调用CourseModuleUpdateView方法 即

def get(self, request, *args, **kwargs):
        formset = self.get_formset()
        return self.render_to_response({'course': self.course, 'formset': formset})

哪个返回有效的HTTP响应。

此响应将到达调用get方法的位置。即在View内部的调度方法。然后从那里返回被调用的响应,即super(CourseModuleUpdateView, self).dispatch(request, *args, **kwargs)。由于您从dispatch方法收到的未返回响应导致

Exception Type: ValueError at /courses/4/module/ Exception Value: The view courses.views.CourseModuleUpdateView didn't return an HttpResponse object. It returned None instead.