django CourseModuleUpdateView未返回HttpResponse对象,它返回None

3ks5zfa0  于 2023-10-21  发布在  Go
关注(0)|答案(1)|浏览(109)

我有以下类用于使用表单集显示相关课程模块

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

eblbsuwk

eblbsuwk1#

根据Django文档,dispatch方法应该返回一个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方法不仅将请求的方法委托给相应的处理程序,它还返回处理程序响应**。
即,在这条线上。

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

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

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

handler将被分配给get。所以在这种情况下,它大致相当于

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

这个get将调用CourseModuleUpdateView类中的get方法,即,

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.

相关问题