form.has_errors标签不起作用 [英] form.has_errors tag not working

查看:36
本文介绍了form.has_errors标签不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Django 1.2.3.我的网站使用 django.contrib.auth.views.login 具有登录功能.输入正确的用户名和密码后,用户便可以登录.但是, form.has_errors 无法正常工作,即,如果输入的登录凭据不正确,我不会看到错误消息.我的template/registration中的login.html如下:

Im using Django 1.2.3. I have login functionality in my site using django.contrib.auth.views.login. The user is able to login after entering correct username and password. But, form.has_errors is not working i.e. if the login credentials entered are incorrect i dont see the error message. My login.html in templates/registration is as follows :

<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>

有什么方法可以解决此问题?

Any way to fix this problem?

请帮助谢谢.

推荐答案

Form 对象确实具有 errors 属性,请阅读

A Form object does have an errors attribute, read the docs for more information:

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

显然, has_errors 在Django 0.95中有效,但至少在1.0以上版本无效(因为它已替换为 errors ).[参考]

Apparently has_errors was valid in Django 0.95 but not from at least 1.0 upwards (as it was replaced with errors). [reference]

这篇关于form.has_errors标签不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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