Django Allauth自定义登录不显示错误 [英] Django Allauth Custom Login Does Not Show Errors

查看:149
本文介绍了Django Allauth自定义登录不显示错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在项目中使用Django Allauth进行用户流程。我已经成功创建了自己的一套模板,它们继承自标准Allauth模板,并且它们可以工作95%。然而,出于某种原因,我没有收到无效登录错误。

I am using Django Allauth for user flow in my project. I have successfully created my own set of templates which inherit from the standard Allauth templates and they work 95%. For some reason, however, I do not receive an error for an invalid login.

如果我为用户输入'test @ email',我会得到'请输入'有效的电子邮件地址'的错误,但如果我输入一个不正确的密码正确的电子邮件,它只是重新加载页面,但没有错误,哪里出了问题。我已经包含了我的模板,其中包含 {{form.login.errors}} {{form.password.errors}} 标签。

If I enter 'test@email' for the user I get a 'Please enter a valid email address' error but if I enter a proper email with an incorrect password it simply reloads the page but with no errors as to what went wrong. I've included my template below which has the {{ form.login.errors }} and {{ form.password.errors }} tags.

所有的指导都表示感谢,谢谢。

All guidance is appreciated, thank you.

模板:

Template:

<div id="search_container">

            <div id="search_box_content">
                {% block page_content %}

                        <h4>Login</h4>

                            <form class="" id="user-form" method="POST" action="{% url 'account_login' %}">

                              {% csrf_token %}
                            <h1> {{ form.login.errors }}</h1>
                            <h1> {{ form.password.errors }}</h1>

                               <div class="input_class">

                                    <label for="login">Username: </label>
                            {{ form.login }}
                            </div>

                            <div class="input_class">
                              <label for="password">Password: </label>
                              {{ form.password }}
                            </div>
                            <div class="input_class" id="forgot_pass">
                              <label for="remember">
                              Remember Me? </label>
                            {{ form.remember }}
                            </div>
                              {% if redirect_field_value %}


                              <input type="hidden" name="{{ redirect_field_name }}" value="{{ redirect_field_value }}" />
                              {% endif %}

                            <div id="modal_bottom_bar">

                              <a class="button secondaryAction" id="forgot_pass" href="{% url 'account_reset_password' %}">Forgot your Password?</a>

                              <button class="primaryAction" id="login_submit" type="submit">Login</button></div>
                            </form>

{% endblock %}

            </div>

        </div>


推荐答案

有些错误是非现场错误,在 field.errors 中不包含它们,而是包含在 form.non_field_errors 中:

Some errors are non field errors such as those errors which are raised for some custom validation and are not included in field.errors rather they are in form.non_field_errors:

所以最好使用这段代码来显示属于字段的所有错误,以及如果您手动呈现表单的自定义验证:

So better use this snippet to show all errors which belongs to fields and to custom validation if you are rendering your form manually:

{% if form.errors %}
    {% for field in form %}
        {% for error in field.errors %}
            <div class="alert alert-error">
                <strong>{{ error|escape }}</strong>
            </div>
        {% endfor %}
    {% endfor %}
    {% for error in form.non_field_errors %}
        <div class="alert alert-error">
            <strong>{{ error|escape }}</strong>
        </div>
    {% endfor %}
{% endif %}

这篇关于Django Allauth自定义登录不显示错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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