user.is_authenticated always returns False for inactive users on template
<p>In my template, <code>login.html</code>, I have:</p>

<pre><code>{% if form.errors %}
    {% if user.is_authenticated %}
    &lt;div class="alert alert-warning"&gt;&lt;center&gt;Your account doesn't have access to this utility.&lt;/center&gt;&lt;/div&gt;
    {% else %}
    &lt;div class="alert alert-warning"&gt;&lt;center&gt;Incorrect username or password!&lt;/center&gt;&lt;/div&gt;
    {% endif %}
{% endif %}
</code></pre>

<p>What I am trying to do is, if after form submission, user is inactive then display a different error message and if user is simply not authenticated then display Incorrect username password error message. This doesn't work. It always displays "Incorrect username or password!" in both the cases. However inside a view, user.is_authenticated returns <code>True</code> even for inactive users.</p>

<p>I there any other wway to get this done? I also tried</p>

<pre><code>{% if 'inactive' in form.errors %}
</code></pre>

<p>But this doesnt work too, even though when I try to print <code>form.errors</code>, it shows text "This account is inactive" for inactive users.</p>

<p>EDIT:
For view, I am simply using django's login view inside a custom login view</p>

<p>views.py:</p>

<pre><code>from django.contrib.auth.views import login, logout
from django.shortcuts import render, redirect

def custom_login(request, **kwargs):
    if request.user.is_authenticated():
        return redirect('/homepage/')
    else:
        return login(request, **kwargs)
</code></pre>

<p>There isn't any point checking <code>{% if user.is_authenticated %}</code> in your login template. If the user is authenticated, then your <code>custom_login</code> view would have redirected them to the homepage.</p>

<p>If the account is inactive, then the form will be invalid and the user will not be logged in. The forms's errors will look like:</p>

<pre><code>{'__all__': [u'This account is inactive.']}
</code></pre>

<p>Therefore checking <code>{% if 'inactive' in form.errors %}</code> will not work, because the error is stored with the key <code>__all__</code>, not <code>inactive</code>.</p>

<p>You could do <code>{% if 'This account is inactive.' in form.non_field_errors %}</code> but this is very fragile, and would break if Django ever changed the text of the error message for inactive users. </p>

<p>It would be better to display the actual errors, rather than trying to find out what sort of error it is in the template. The easiest way to display non-field errors is to include:</p>

<pre><code>{{ form.non_field_errors }}
</code></pre>

<p>Or, if you need more control:</p>

<pre><code>{% for error in form.non_field_errors %}
    {{ error }}
{% endfor %}
</code></pre>

<p>If you need to change the error message for inactive users, you can subclass the authentication form, then use that in your login view.</p>

<pre><code>my_error_messages = AuthenticationForm.error_messages.copy()
my_error_messages['inactive'] = 'My custom message'

class MyAuthenticationForm(AuthenticationForm):
    error_messages = my_error_messages
</code></pre>

python django django-templates django-authentication