【问题标题】:The view didn't return an HttpResponse object. It returned None instead该视图未返回 HttpResponse 对象。它返回 None 而不是
【发布时间】:2014-12-03 05:33:43
【问题描述】:

我有以下简单的看法。为什么会导致这个错误?

The view auth_lifecycle.views.user_profile didn't return an HttpResponse object. It returned None instead.

"""Renders web pages for the user-authentication-lifecycle project."""
from django.shortcuts               import render
from django.template                import RequestContext
from django.contrib.auth            import authenticate, login

def user_profile(request):
    """Displays information unique to the logged-in user."""

    user = authenticate(username='superuserusername', password='sueruserpassword')
    login(request, user)

    render(request, 'auth_lifecycle/user_profile.html',
           context_instance=RequestContext(request))

【问题讨论】:

    标签: python django django-views


    【解决方案1】:

    因为视图必须返回 render,而不仅仅是调用它。将最后一行改为

    return render(request, 'auth_lifecycle/user_profile.html',
               context_instance=RequestContext(request))
    

    【讨论】:

      【解决方案2】:

      我在使用 UpdateView 时遇到了同样的错误

      我有这个:

      if form.is_valid() and form2.is_valid():
          form.save()
          form2.save()
          return HttpResponseRedirect(self.get_success_url())
      

      我解决了只是做:

      if form.is_valid() and form2.is_valid():
          form.save()
          form2.save()
          return HttpResponseRedirect(reverse_lazy('adopcion:solicitud_listar'))
      

      【讨论】:

        【解决方案3】:
        if qs.count()==1:
                print('cart id exists')
                if ....
        
        else:    
                return render(request,"carts/home.html",{})
        

        这种类型的代码也会返回同样的错误,这是因为 作为 return 语句的意图应该是 for else 而不是 if 语句。

        上面的代码可以改成

        if qs.count()==1:
                print('cart id exists')
                if ....
        
        else:   
        
        return render(request,"carts/home.html",{})
        

        这可能会解决此类问题

        【讨论】:

        • 你在说什么,2个代码sn-ps是一样的
        • 我遇到了问题中提到的相同错误,但由于第一个代码中的意图,return 语句用于第二个 if 语句,但不是第一个,这就是我的原因得到“那个视图没有返回一个 HttpResponse 对象。它返回了 None 。”当我将它设置为第一个 if 语句时,它就起作用了
        • @MuhammadOmerAslam:Python 对代码缩进很敏感。由于第一个版本中的代码缩进,return .. render() 仅在 if ..else 的else... 部分中调用。在第二个版本中,没有缩进,所以return render()... 总是执行。
        【解决方案4】:

        Python 对缩进非常敏感,下面的代码我得到了同样的错误:

            except IntegrityError as e:
                if 'unique constraint' in e.args:
                    return render(request, "calender.html")
        

        正确的缩进是:

            except IntegrityError as e:
                if 'unique constraint' in e.args:
                return render(request, "calender.html")
        

        【讨论】:

        • 这不可能
        【解决方案5】:

        我知道在这里发布内容已经很晚了,但这可能会帮助某人找出愚蠢的错误。

        您有可能在render() 之前错过了return。请确保。

        【讨论】:

          猜你喜欢
          • 2020-11-01
          • 2021-06-22
          • 2019-06-12
          • 2018-06-02
          • 2020-08-19
          • 1970-01-01
          • 2017-12-28
          • 1970-01-01
          相关资源
          最近更新 更多