@login_required decorator only works on first page after login

I am trying to use @login_required to only allow login in users to access various views.

My code for the page login directs the user to after logging in is:

@login_required
def wheel(request):
    return render(request, "ISO22301/wheel.html",)

If I the select a link to navigate to another page I get:

Internal Server Error: /ISO22301/introduction/
Traceback (most recent call last):
  File "/Users/beyondscorecard/Documents/GitHub/Resilience/.venv/lib/python3.12/site-packages/django/core/handlers/exception.py", line 55, in inner
    response = get_response(request)
               ^^^^^^^^^^^^^^^^^^^^^
  File "/Users/beyondscorecard/Documents/GitHub/Resilience/.venv/lib/python3.12/site-packages/django/core/handlers/base.py", line 197, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/beyondscorecard/Documents/GitHub/Resilience/.venv/lib/python3.12/site-packages/django/contrib/auth/decorators.py", line 60, in _view_wrapper
    return view_func(request, *args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/beyondscorecard/Documents/GitHub/Resilience/ISO22301/views.py", line 24, in introduction
    create_context=createheader(0) #pass starting values to use to extract desired text, get tuple
                   ^^^^^^^^^^^^^^^
  File "/Users/beyondscorecard/Documents/GitHub/Resilience/.venv/lib/python3.12/site-packages/django/contrib/auth/decorators.py", line 57, in _view_wrapper
    test_pass = test_func(request.user)
                          ^^^^^^^^^^^^
AttributeError: 'int' object has no attribute 'user'
[28/Sep/2024 20:09:30] "GET /ISO22301/introduction/ HTTP/1.1" 500 73338

The wheel template code (png code deleted):

{% load static %}

<!DOCTYPE html>
<html lang = 'en'>
    <style>
    #path_rm {
        fill: red;
        opacity: .5;

    </style>
<meta name='viewport' content='width=device-width, initial-scale=1'>
<head>
    <link rel='stylesheet' href='{% static 'css/styles.css' %}'>

</head>
<h1>Resilience</h1>
<body>
<!--   
    <img src='{% static 'images/ISO_Map2.svg' %}' alt='ISO Map'  usemap='#ISO'>
    </map> -->   
    <div class='containervert' style='top: 5%;'>
        <?xml version="1.0" encoding="UTF-8"?>
        <!-- Generated by Pixelmator Pro 3.6.9 -->
        <svg width="720" height="688" viewBox="0 0 720 688" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">

           <path id="path_center" fill="#027aff" stroke="none" opacity="0" d="M 457 331.5 C 457 278.204529 413.795502 235 360.500092 235 C 307.204498 235 264 278.204529 264 331.5 C 264 384.795471 307.204498 428 360.500092 428 C 413.795502 428 457 384.795471 457 331.5 Z"/>
            <path id="path_cm" fill="#027aff" stroke="none" opacity="0" d="M 708.919983 329.78241 C 708.919983 310.452454 693.249939 294.78241 673.920044 294.78241 C 654.590027 294.78241 638.919983 310.452454 638.919983 329.78241 C 638.919983 349.112366 654.590027 364.78241 673.920044 364.78241 C 693.249939 364.78241 708.919983 349.112366 708.919983 329.78241 Z"/>
            <path id="path_rm" fill="#027aff" stroke="none" opacity="0" d="M 676.919983 195.78241 C 676.919983 176.452454 661.249939 160.78241 641.920044 160.78241 C 622.590027 160.78241 606.919983 176.452454 606.919983 195.78241 C 606.919983 215.112366 622.590027 230.78241 641.920044 230.78241 C 661.249939 230.78241 676.919983 215.112366 676.919983 195.78241 Z"/>
            <path id="path_is" fill="#027aff" stroke="none" opacity="0" d="M 532.919983 52.78241 C 532.919983 33.452454 517.249939 17.78241 497.920013 17.78241 C 478.589996 17.78241 462.919983 33.452454 462.919983 52.78241 C 462.919983 72.112366 478.589996 87.78241 497.920013 87.78241 C 517.249939 87.78241 532.919983 72.112366 532.919983 52.78241 Z"/>
            <a href='{% url 'introduction' %}'>
            <path id="path_bc" fill="#027aff" stroke="none" opacity="0" d="M 210.919983 80.78241 C 210.919983 61.452454 195.249954 45.78241 175.920013 45.78241 C 156.590012 45.78241 140.919983 61.452454 140.919983 80.78241 C 140.919983 100.112366 156.590012 115.78241 175.920013 115.78241 C 195.249954 115.78241 210.919983 100.112366 210.919983 80.78241 Z"/>
            </a>
            <path id="path_hr" fill="#027aff" stroke="none" opacity="0" d="M 82.919983 332.78241 C 82.919983 313.452454 67.249954 297.78241 47.920013 297.78241 C 28.59001 297.78241 12.919983 313.452454 12.919983 332.78241 C 12.919983 352.112366 28.59001 367.78241 47.920013 367.78241 C 67.249954 367.78241 82.919983 352.112366 82.919983 332.78241 Z"/>
            <path id="path_ict" fill="#027aff" stroke="none" opacity="0" d="M 110.919983 467.78241 C 110.919983 448.452454 95.249954 432.78241 75.920013 432.78241 C 56.590012 432.78241 40.919983 448.452454 40.919983 467.78241 C 40.919983 487.112366 56.590012 502.78241 75.920013 502.78241 C 95.249954 502.78241 110.919983 487.112366 110.919983 467.78241 Z"/>
            <path id="path_sc" fill="#027aff" stroke="none" opacity="0" d="M 195.919983 568.78241 C 195.919983 549.452454 180.249954 533.78241 160.920013 533.78241 C 141.590012 533.78241 125.919983 549.452454 125.919983 568.78241 C 125.919983 588.112366 141.590012 603.78241 160.920013 603.78241 C 180.249954 603.78241 195.919983 588.112366 195.919983 568.78241 Z"/>
            <path id="path_fa" fill="#027aff" stroke="none" opacity="0" d="M 312.919983 628.78241 C 312.919983 609.452454 297.249969 593.78241 277.920013 593.78241 C 258.589996 593.78241 242.919983 609.452454 242.919983 628.78241 C 242.919983 648.112366 258.589996 663.78241 277.920013 663.78241 C 297.249969 663.78241 312.919983 648.112366 312.919983 628.78241 Z"/>
            <path id="path_env" fill="#027aff" stroke="none" opacity="0" d="M 473.919983 635.78241 C 473.919983 616.452454 458.249969 600.78241 438.920013 600.78241 C 419.589996 600.78241 403.919983 616.452454 403.919983 635.78241 C 403.919983 655.112366 419.589996 670.78241 438.920013 670.78241 C 458.249969 670.78241 473.919983 655.112366 473.919983 635.78241 Z"/>
            <path id="path_hs" fill="#027aff" stroke="none" opacity="0" d="M 593.919983 574.78241 C 593.919983 555.452454 578.249939 539.78241 558.920044 539.78241 C 539.590027 539.78241 523.919983 555.452454 523.919983 574.78241 C 523.919983 594.112366 539.590027 609.78241 558.920044 609.78241 C 578.249939 609.78241 593.919983 594.112366 593.919983 574.78241 Z"/>
            <path id="path_em" fill="#027aff" stroke="none" opacity="0" d="M 676.919983 467.78241 C 676.919983 448.452454 661.249939 432.78241 641.920044 432.78241 C 622.590027 432.78241 606.919983 448.452454 606.919983 467.78241 C 606.919983 487.112366 622.590027 502.78241 641.920044 502.78241 C 661.249939 502.78241 676.919983 487.112366 676.919983 467.78241 Z"/>
        </svg>
    
    
</div>
</body>
</html>

What is the link that you are selecting?

What is the code for the view of that page? (The view that handles the /ISO22301/introduction/ url?)

Here’s the code:

@login_required
def introduction(request):
    create_context=createheader(0) #pass starting values to use to extract desired text, get tuple
    context = create_context[0] #get context to pass 
    return render(request, "ISO22301/introduction.html",context)

What is the createheader function?

It creates a flexbox graphic that is used by a number of templates. The value passed 0 in this case) Is used to identify which ISO is surveyed:

def createheader(n): # n is the value to use in lists created here, refers to teh ISO being generated

    #Name of browser tab
    browsertab=list(Dashboard.objects.values_list('company', flat=True))
    browsertab=browsertab[n]
        #Name of dashboard
    dashboard_title=list(Dashboard.objects.values_list('dashboard', flat=True))
    dashboard_title=dashboard_title[n]
    # names and text for both ends of LoO to extract value for current project
    project=list(Project.objects.values_list('project', flat=True))
    projecttext=list(Project.objects.values_list('project_text', flat=True))
    outcome=list(Project.objects.values_list('outcome', flat=True))
    outcometext=list(Project.objects.values_list('outcome_text', flat=True))
    # Used to identify specific text for ISO
    project=project[n]
    projecttext=projecttext[n]
    outcome=outcome[n]
    outcometext=outcometext[n]
    #layout data
    areaheader=Area_Header.objects.all() # get names of areas for header
    column_header=Column_Header.objects.all() #headers for table columns
    num_choices=list(Column_Header.objects.values_list('colcount',flat=True)) #h number of unique valoes0 to n
    num_choices_total=num_choices.count(1)
    nchoices = []
    for n in range(num_choices_total):
        nchoices.append(n)
    areatopics = Area_Topic.objects.all() #used in layout to enter topics in flexbox
    areas =  Area.objects.all().prefetch_related('question_set') #Get list of areas tied to question set
    areaheader = Area.objects.values_list('area', flat=True)
    textname= ["Text"] #Comment feild text
    context_header = {
        "textname": textname,
        "area": areas,
        "areatopics": areatopics,
        "areaheader": areaheader,
        "browsertab": browsertab,
        "dashboard_title": dashboard_title,
        "project": project,
        "projecttext": projecttext,
        "outcome": outcome,
        "outcometext": outcometext,
        "nchoices": nchoices,
        "columnheader": column_header,
        }
    return context_header, nchoices

I’m having a hard time reconciling this code with the traceback you’ve posted.

Please post the introduction.html template.

If you’re running this using runserver in a development environment, I suggest deleting all .pyc files that may currently be in your project.

What version of Django are you using?

django==5.1

Template is mostly text placeholders:

{% extends "ISO22301/layout.html" %}

{% block content %}
<style>
</style>
{% csrf_token %}
<h1>Business Continuity Management</h1>
<div class=t4>


 <p>The outcomes of maintaining a BCMS are shaped by the organization’s legal, regulatory, organizational and industry requirements, products and services provided, processes employed, size and structure of the organization, and the requirements of its interested parties.<p>

<p>A BCMS emphasizes the importance of:</p>
    <ul>
    <li>understanding the organization’s needs and the necessity for establishing business continuity policies and objectives;</li>
    <li>operating and maintaining processes, capabilities and response structures for ensuring the organization will survive disruptions;</li>
    <li>monitoring and reviewing the performance and effectiveness of the BCMS; — continual improvement based on qualitative and quantitative measures.</li>
    </ul>
<p>A BCMS, like any other management system, includes the following components:</p>
<ol type=a>
    <li>a policy;</li>
    <li>competent people with defined responsibilities;</li>
    <li>management processes relating to: 
        <ol>
            <li>policy;</li>
            <li>planning;</li>
            <li>implementation and operation;</li>
            <li>performance assessment;</li>
            <li>management review;</li>
            <li>continual improvement;</li>
        </ol>
    </li> 
    <li>documented information supporting operational control and enabling performance evaluation. </li>
</div>
<button input type='button' name=Begin Survey onclick="location.href='{% url 'results' %}'"><lable>Begin Survey</lable></button>
{% endblock %}

Is that traceback you posted the complete traceback or is it the second of two consecutive errors with traceback for the same request? (There’s definitely something happening here that isn’t currently being shown, but I don’t know what it might be.)

Ok, now it’s working. I deleted all the @login_required and then reentered them and it works - pages rendered if logged in and return login page if not. PFM…