【问题标题】:form.has_errors tag not workingform.has_errors 标签不起作用
【发布时间】:2011-01-06 12:12:47
【问题描述】:

我正在使用 Django 1.2.3。我在我的网站中使用django.contrib.auth.views.login 登录功能。用户输入正确的用户名和密码后即可登录。但是,form.has_errors 不起作用,即如果输入的登录凭据不正确,我看不到错误消息。我在模板/注册中的 login.html 如下:

<html>
    <head>
        <title>Login</title>
    </head>
    <body>
        <h1>User Login</h1>
        {% if form.has_errors %}
            <p>Your username and password didn't match. Please try again.</p>
        {% endif %}
        <form method="post" action=".">
            {% csrf_token %}
            <p><label for="id_username">Username:</label> {{ form.username }}</p>
            <p><label for="id_password">Password:</label> {{ form.password }}</p>
            <input type="hidden" name="next" value="/" />
            <input type="submit" value="login" />
        </form>
    </body>
</html>

有什么办法可以解决这个问题?

请帮忙 谢谢。

【问题讨论】:

    标签: python django


    【解决方案1】:

    Form 对象确实具有 errors 属性,请阅读 the docs 了解更多信息:

    {% if form.errors %}
        <p>Your username and password didn't match. Please try again.</p>
    {% endif %}
    

    显然has_errors 在 Django 0.95 中有效,但至少在 1.0 以上时无效(因为它已被 errors 取代)。 [reference]

    【讨论】:

    猜你喜欢
    • 2017-01-01
    • 2012-04-28
    • 2016-05-14
    • 1970-01-01
    • 1970-01-01
    • 2017-08-24
    • 2011-08-03
    相关资源
    最近更新 更多