2013-12-08 45 views
1

我的登录工作正常,但没有显示所有错误。当我输入无效的用户名或密码时,这些错误不会显示,并且页面只是刷新而不会放错任何错误。django authenticationform不显示所有错误

然而,当我离开一个领域的空白,它显示正确的错误:

因此丢失的错误(从源代码):

error_messages = { 
    'invalid_login': _("Please enter a correct %(username)s and password. " 
         "Note that both fields may be case-sensitive."), 
    'inactive': _("This account is inactive."), 
} 

我的代码:

def login_user(request): 
    """Logs a user into the application.""" 
    auth_form = AuthenticationForm(None, request.POST or None) 

    # The form itself handles authentication and checking to make sure password and such are supplied. 
    if auth_form.is_valid(): 
     (request, auth_form.get_user()) 
     return HttpResponseRedirect(reverse('index')) 

    return render(request, 'login.html', {'auth_form': auth_form}) 

我模板:

<form action="{% url 'login_user' %}" method="post" class="login">{% csrf_token %} 
    <div> 
     <input name="username" placeholder="Username:" type="text" name="username"  value="" id="username" class="login"> 
     {{ auth_form.username.errors }} 
    </div> 
    <div> 
     <input name="password" placeholder="Password:" type="password" name="password" value="" id="password" class="login"> 
     {{ auth_form.password.errors }} 
    </div> 
    <div> 
     <center> 
      <a href="{% url 'register_user' %}"> 
       register 
      </a> 
      &nbsp;&nbsp;&nbsp;&nbsp; 
      <button type="submit" class="link"> 
       login 
      </button> 
     </center> 
    </div> 
</form> 

我做错了什么?

回答

1

您不包括form.non_field_errors在您的模板。有关示例,请参阅customizing the form template上的文档。

另外,AuthenticationForm将请求作为第一个参数。看起来有点奇怪,你通过None而不是request