Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django Allauth Custom Login Does Not Show Errors

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.

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>
like image 672
apardes Avatar asked Jun 17 '14 21:06

apardes


1 Answers

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 %}
like image 188
Aamir Rind Avatar answered Oct 01 '22 04:10

Aamir Rind