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>Just to complement Alasdair's very sensible answer, if you <strong>really</strong> want to explicitely check whether the user exists but is inactive, you can use <code>AuthenticationForm.get_user()</code>, ie:</p>

<pre><code>{% if form.errors %}
  {% with form.get_user as user %}
    {% if user %}
       {# the user is inactive #} 
    {% else %}
       {# no user matching username/password #} 
    {% endif %}
  {% endwith %}
{% endif %} 
</code></pre>

<p>This is assuming you're using the default <code>django.contrib.auth.forms.AuthenticationForm</code> of course - you can use your own and override the <code>confirm_login_allowed()</code> to implement your own policy.</p>

python django django-templates django-authentication