Hans de Jong
Hans de Jong

Reputation: 2078

django authenticationform not showing all errors

My login works fine except for not showing all errors. When i type an invalid username or password, these errors don't show and the page just refreshes without putting any errors down.

However when i leave one field blank, it shows the correct error:

So the missing errors are(from source):

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."),
}

my code:

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

My template:

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

what do i do wrong?

Upvotes: 2

Views: 1286

Answers (1)

Alasdair
Alasdair

Reputation: 309069

You aren't including form.non_field_errors in your template. See the docs on customizing the form template for an example.

As an aside, the AuthenticationForm takes the request as its first argument. It looks slightly strange that you are passing None instead of request.

Upvotes: 1

Related Questions