【问题标题】:The view didn't didn't return an HttpResponse object. It returned None instead视图没有返回 HttpResponse 对象。它返回 None 而不是
【发布时间】:2017-12-28 13:19:53
【问题描述】:

这是我的意见.py

views.py
def user_login(request):
    if request.method  == 'POST':
        username = request.POST['username']
        password = request.POST['password']
        user = authenticate(username = username , password = password)
        if user:
            if user.is_active:
                login(request , user)
                return HttpResponseRedirect('/zdorovo/')
            else:
                return HttpResponse('You dont have an account with us')
        else:
            print "Invalid Login Details: {0} , {1}".format(username , password)
            return HttpResponse('Invalid Login Details ')

    else:
        render(request , 'zdorovo/login.html' , {})

authenticate 、 login 、 HttpResponse 、 HttpResponseRedirect 已正确导入。我还处理了 Indentation ,所以错误并没有从那一端引发。 请帮助我理解错误。

这是我的 login.html

<body>
  <h1>Login</h1>
  <form id="login_id" method="post" action="/zdorovo/login/">
    {% csrf_token %}
    Username: <input type="text" name="username"  value=""/>
    <br/>
    Password: <input type="password" name="password" value=""/>
    <br/>
    <input type="submit" value="Login" />
  </form>
</body>

P.S.:- 如果您需要我的其他脚本,请更新我。

【问题讨论】:

  • 在最后一个else条件中使用return
  • 它现在可以工作了,谢谢。
  • 我会将其作为答案发布,以便对其他人有所帮助。

标签: django django-views


【解决方案1】:

您的函数在执行最后一个else 条件时不会返回任何内容,您应该添加return 语句。

return render(request , 'zdorovo/login.html' , {})

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-10-06
    • 2017-01-25
    • 2020-12-03
    • 2018-09-28
    • 2017-08-01
    • 2019-06-18
    • 2020-09-29
    相关资源
    最近更新 更多