Partho Debnath
Partho Debnath

Reputation: 55

Django AuthenticationForm Error messages not showing

My question is when I give the wrong email or password, No error message is Shown, but why ?? and how can I fix that?

This is my signin View function

def signin(request):
    form = AuthenticationForm(request=request, data=request.POST)
    
    if form.is_valid() == True:
        user = authenticate(email=form.cleaned_data['username'], password=form.cleaned_data['password'])
        if user is not None:
            login(request, user)
        return HttpResponse(f'{request.user}')
    else:
        context = {'form': form}
    return render(request, 'signin.html', context)

This is my Html

<form action="{% url 'signin' %}" method="post">
        {% csrf_token %}
           
        {% for field in form %}
            {{ field.errors }}
            {{ field }} <br>
        {% endfor %}

        <button type="submit">Signin</button>
    </form>

Upvotes: 1

Views: 467

Answers (1)

David S.
David S.

Reputation: 463

I think you need to add {{ form.non_field_errors }} to your template. This is because the error raised by unsuccessful authentication is not associated to a specific field, so it isn't included in any field's errors. Reference

Upvotes: 1

Related Questions