HTTP请求对象不响应 [英] HTTP Request Object does not respond

查看:180
本文介绍了HTTP请求对象不响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在Django登录表单。
当我点击登录提交按钮时,它给出错误



ValueError at / registration /
视图注册。 .login没有返回一个HttpResponse对象。



视图文件是

从django.shortcuts导入render
从registration.models import注册
从django.http导入HttpResponse
def login(请求):
如果request.method =='POST':
user = authenticate(username = request.POST ['username'],password = request.POST ['password'])
如果用户不是没有:
如果user.is_active:
登录(请求,用户)
#success
返回HttpResponseRedirect('sucess')
else:
#disabled account
return direct_to_template(request,'inactive_account.html')
else:
#无效登录
返回direct_to_template(请求, 'invalid_login.html'

def logout(request)
logout(request)
return direct_to_template(request,'logged_out.html')

和login.html文件是

 < H3>登录< / H3> 
< form action =/ login /method =postaccept-charset =utf-8>
< label for =username>用户名: - < / label>< input type =textname =usernamevalue =id =username/>< BR>
< label for =password>密码: - < / label>< input type =passwordname =passwordvalue =id =password/>
< p>< input type =submitvalue =Login→>< / p>
< / form>


解决方案

您的登录视图将返回一个POST请求的HttpResponse只有(在表单提交)。当您将浏览器寻址到登录页面时,它会产生一个GET请求,这不是在您的视图中处理



我建议您阅读有关用户验证的更多信息:一个href =https://docs.djangoproject.com/en/dev/topics/auth/#how-to-log-a-user-in =nofollow> doc 。



如上所述,使用 render render_to_response from django.shortcuts


I am making a login form in Django. When I am click on login submit button , It gives an error of

ValueError at /registration/ The view registration.views.login didn't return an HttpResponse object.

The view file is

from django.template import  loader
from django.shortcuts import render
from registration.models import Registration
from django.http import HttpResponse
def login(request):
  if request.method == 'POST':
    user = authenticate(username=request.POST['username'], password=request.POST['password'])
    if user is not None:
      if user.is_active:
        login(request, user)
        # success
        return HttpResponseRedirect('sucess')
      else:
        # disabled account
        return direct_to_template(request, 'inactive_account.html')
    else:
      # invalid login
      return direct_to_template(request, 'invalid_login.html')

def logout(request):
  logout(request)
  return direct_to_template(request, 'logged_out.html')

and login.html file is

<h3>Login</h3>
<form action="/login/" method="post" accept-charset="utf-8">
<label for="username">Username:--</label><input type="text" name="username" value="" id="username" /><br>
<label for="password">Password:-- </label><input type="password" name="password" value="" id="password" />
<p><input type="submit" value="Login →"></p>
</form>

解决方案

Your login view will return a HttpResponse for a POST request only (on form submit). When you address your browser to login page, it makes a GET request, which is not handled in your view

I`ll recomend you to read more about user athentication: doc.

And as mentioned, use render or render_to_response functions from django.shortcuts

这篇关于HTTP请求对象不响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆