【问题标题】:django authenticationform not showing all errorsdjango authenticationform 没有显示所有错误
【发布时间】:2013-12-25 16:47:39
【问题描述】:

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

但是,当我将一个字段留空时,它会显示正确的错误:

所以缺少的错误是(来自源):

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>

我做错了什么?

【问题讨论】:

    标签: django forms authentication


    【解决方案1】:

    您的模板中没有包含form.non_field_errors。有关示例,请参阅 customizing the form template 上的文档。

    顺便说一句,AuthenticationForm 将请求作为其第一个参数。您传递的是None 而不是request,这看起来有点奇怪。

    【讨论】:

      猜你喜欢
      • 2017-06-26
      • 1970-01-01
      • 2015-08-24
      • 1970-01-01
      • 2021-12-29
      • 2013-01-03
      • 2018-08-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多