if user.is_authenticated not working as expected on all pages

I have a base.html page with the same navbar on all pages. I am using {{user}} by the seachbar to try and see what is happening. The site is setup to direct anyone not logged in to the login page

When I am not logged in, and go to the page, the user reported is anonymoususer, and login shows up, logout does not. As expected.

After logging in as admin, the user reported is 1, and login is still in the navbar. Logout does not appear.

When going to any other page on the site, user reported is admin. Everything works as expeted. Login shows up and login does not.

<nav class="navbar navbar-expand-lg navbar-light bg-light">
    <div class="container-fluid">
      <a class="navbar-brand" href="#">{LK}</a>
      <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
        <span class="navbar-toggler-icon"></span>
      </button>
      <div class="collapse navbar-collapse" id="navbarSupportedContent">
        <ul class="navbar-nav me-auto mb-2 mb-lg-0">
          <li class="nav-item">
            <a class="nav-link active" aria-current="page" href="{% url 'home' %}">Home</a>
          </li>
          
          {% if user.is_authenticated %}
          <li class="nav-item">
            <a class="nav-link" href="{% url 'logout' %}">Logout</a>
          </li>
          {% endif %}
          
          
          
          {% if not user.is_authenticated %}
          
            <li class="nav-item">
              <a class="nav-link" href="{% url 'login' %}">Login</a>
            </li>
            {% endif %}
            
            <!-- <li class="nav-item">
              <a class="nav-link" href="{% url 'register' %}">Register</a>
            </li> -->
          
            
           





            
          <!-- <li class="nav-item dropdown">
            <a class="nav-link dropdown-toggle" href="#" role="button" data-bs-toggle="dropdown" aria-expanded="false">
              Customers
            </a>
            <ul class="dropdown-menu" aria-labelledby="navbarDropdownMenuLink">
              <li><a class="dropdown-item" href="{% url 'customers:customer_list' %}">Customer List</a></li>
              <li><a class="dropdown-item" href="#">Add Customer</a></li>
              <li><a class="dropdown-item" href="">Add Customer Contact</a></li>
            </ul> 
          </li> -->
          
          <li class="nav-item dropdown">
            <a class="nav-link dropdown-toggle" href="#" role="button" data-bs-toggle="dropdown" aria-expanded="false">
              Workorders
            </a>
            <ul class="dropdown-menu" aria-labelledby="navbarDropdownMenuLink">
              <li><a class="dropdown-item" href="{% url 'workorders:workorder_list' %}">Workorder List</a></li>
              <li><a class="dropdown-item" href="{% url 'workorders:createbase' %}">Add Workorder</a></li>
              <li><a class="dropdown-item" href="{% url 'workorders:quote_list' %}">Quote List</a></li>
              <!-- <li><a class="dropdown-item" href="">Add Customer Contact</a></li> -->
            </ul> 
          </li>
          <li class="nav-item dropdown">
            <a class="nav-link dropdown-toggle" href="#" role="button" data-bs-toggle="dropdown" aria-expanded="false">
              Finance
            </a>
            <ul class="dropdown-menu" aria-labelledby="navbarDropdownMenuLink">
              <li><a class="dropdown-item" href="{% url 'finance:ar_dashboard' %}">A/R Dashboard</a></li>
              
              
          
              
              <!-- <li><a class="dropdown-item" href="">Add Customer Contact</a></li> -->
            </ul> 
          </li>

          <li class="nav-item dropdown">
            <a class="nav-link dropdown-toggle" href="#" role="button" data-bs-toggle="dropdown" aria-expanded="false">
              Customers
            </a>
            <ul class="dropdown-menu" aria-labelledby="navbarDropdownMenuLink">
              <li><a class="dropdown-item" href="{% url 'customers:customer_dashboard' %}">Customer Detail</a></li>
              
              
          
              
              <!-- <li><a class="dropdown-item" href="">Add Customer Contact</a></li> -->
            </ul> 
          </li>
          <li class="nav-item dropdown">
            <a class="nav-link dropdown-toggle" href="#" role="button" data-bs-toggle="dropdown" aria-expanded="false">
              Administration
            </a>
            <ul class="dropdown-menu" aria-labelledby="navbarDropdownMenuLink">
              <li><a class="dropdown-item" href="{% url 'pricesheet:template_list' %}">Price Templates</a></li>
              
              <li><a class="dropdown-item" href="{% url 'pdf:management' %}">PDF Options</a></li>
          
                <li><a class="dropdown-item" href="/admin">Admin</a></li>
              
              <!-- <li><a class="dropdown-item" href="">Add Customer Contact</a></li> -->
            </ul> 
          </li>
          <li class="nav-item dropdown">
            <a class="nav-link dropdown-toggle" href="#" role="button" data-bs-toggle="dropdown" aria-expanded="false">
              Vendors
            </a>
            <ul class="dropdown-menu" aria-labelledby="navbarDropdownMenuLink">
              <li><a class="dropdown-item" href="{% url 'inventory:add_vendor' %}">Add Vendor</a></li>
              <li><a class="dropdown-item" href="{% url 'inventory:vendor_list' %}">Vendor List</a></li>
              <!-- <li><a class="dropdown-item" href="">Add Customer Contact</a></li> -->
            </ul> 
          </li>
          
          <li class="nav-item dropdown">
            <a class="nav-link dropdown-toggle" href="#" role="button" data-bs-toggle="dropdown" aria-expanded="false">
              PDF Tests
            </a>
            <ul class="dropdown-menu" aria-labelledby="navbarDropdownMenuLink">
              <li><a class="dropdown-item" href="{% url 'pdf:showitems' %}">Item list w/ xhtml2pdf</a></li>
              <!-- <li><a class="dropdown-item" href="">Add Customer Contact</a></li> -->
            </ul> 
          </li>
          
      
        </ul>
        {{ user }}
        <form action="{% url 'search' %}" method="GET" class="d-flex" role="search">
          <input class="form-control me-2" name="q" type="search" placeholder="Search" aria-label="Search">
          <button class="btn btn-outline-success" type="submit">Search</button>
        </form>
        
     
      </div>
    </div>
  </nav>
@unauthenticated_user
def login_view(request):
    if request.method == "POST":
        form = AuthenticationForm(request, data=request.POST)
        if form.is_valid():
            user = form.get_user()
            login(request, user)
            return redirect('/')
        #print(user)
    else:
        form = AuthenticationForm(request)
    context = {
        "form": form
    }
    return render(request, "accounts/login.html", context)

unauthenticated user decorator

def unauthenticated_user(view_func):
    def wrapper_func(request, *args, **kwargs):
        if request.user.is_authenticated:
            return redirect('home')
        else:
            return view_func(request, *args, **kwargs)
    return wrapper_func

home view (I realize this is redundant and can be skipped. The same problem happens if this is skipped and user goes directly to dashboard:dashboard

@login_required
def home_view(request, id=None):
    if request.user.is_authenticated:
        return redirect('dashboard:dashboard')
    else:
        return redirect('accounts/login')

dashboard:dashboard

@login_required
def dashboard(request, id=None):
    user = request.user.id
    items = WorkorderItem.objects.filter(assigned_user_id = user).exclude(completed=1).order_by("-workorder")
    status = JobStatus.objects.all()
    print(user)

    context = {
        'items':items,
        'user':user,
        'status':status,
    }
    return render(request, "dashboard/employee_dashboard.html", context)

employee_dashboard.html

{% extends "base.html" %}
{% load static %}
{% load crispy_forms_tags %}

{% block content %}
...
{% endblock %}

Are you aware that you are not checking the password here? (Are you checking it in the form validation?) See How to log a user in.

Is this always happening on the first request only, following the login?

Did you actually mean to say here that “Logout shows up and login does not.”?

What versions of Django and Python are you using?

I changed the login. It was checking the password somewhere in the original, but I’m not sure how.

New view:

@unauthenticated_user
def login_view(request):
    if request.method == "POST":
        # form = AuthenticationForm(request, data=request.POST)
        # if form.is_valid():
        #     user = form.get_user()
        #     login(request, user)
        #     return redirect('/')
        # #print(user)
        username = request.POST["username"]
        password = request.POST["password"]
        user = authenticate(request, username=username, password=password)
        if user is not None:
            login(request, user)
            return redirect('/')
    else:
        form = AuthenticationForm(request)
    context = {
        "form": form
    }
    return render(request, "accounts/login.html", context)

The navbar is wrong every time I go to the dashboard page. All other pages work as expected.

Yes, I mistyped. Logout shows up and login does not

Django version 4.2.9
Python version 3.8.10

The issue is here:

combined with this:

The standard auth context processor adds user to the context as the user object - not just the PK.

That’s what allows the expression user.is_authenticated to work. If user is the integer pk, then that expression will always be false.

Yep… that’s exactly what it was. Thank you