django parameter causing the wrong template and view to render

386 views Asked by At

Overview / Problem:

Hi! The wrong template and view are loading every time I click the check_tier_level link in my template.

When that parameter is in, it loads the home view with "check_tier_level" as the special_message, even though my links go to the view for check_tier_level. If I click any of the form buttons to grant access, the proper message shows up in that spot. I just can't check the level.

The app works fine and renders the right template / view only when I remove the special_message parameter from the urlpattern and view.

The only other lead I have on this is that the url in the browser will also look like http://127.0.0.1:8000/tiered_access_app/Tier 1 granted!/, instead of having the characters escaped with %20 and so on.

My goal

The whole reason I want to keep that parameter in is so a special_message can notify users of the latest update based on their actions. If anyone knows a better way to do this without making a whole new view / template (which I know is a solution, and how to do it), I'd like to know how. Anyways, here's my code:

urlpatterns.py

path('', views.home, name='home'),
path('<str:special_message>/', views.home, name='home_special_message'),
path('check_tier_level/', views.check_tier_level, name='check_tier_level'),
path('check_tier_level/gain_access/', views.gain_access, name='gain_access'),

views.py

def home(request, special_message=None):
    return render(request, 'tiered_access_app/home.html', {'special_message': special_message})

def check_tier_level(request):
    current_user = request.user
    try:
        find_user = TieredAppCustomUser.objects.get(user=current_user)
        if find_user.tier_choice == 'tier1':
            return render(request, 'tiered_access_app/check_tier_level.html', {'level_1': 'You have access to level 1.'})
        # and so on with other levels...
    except ObjectDoesNotExist:
        pass
    return render(request, 'tiered_access_app/check_tier_level.html', {'no_access': 'You don\'t have access to the content here yet.'})

home.html

{% if special_message %}
    <h2>{{ special_message }}</h2>
{% endif %}

<form action="{% url 'tiered_access_app:gain_access' %}" method="POST">
    {% csrf_token %}
    <label>Check level 1 access</label>

        <!-- *******PROBLEM WITH LINK HERE******** -->
    <p><a href="{% url 'tiered_access_app:check_tier_level' %}">Try to access level 1 first.</a> You won't be allowed unless you gain access first, by clicking the button below.</p>
        <!-- *******PROBLEM WITH LINK HERE******** -->

    <input type="hidden" value='1' name="tier_level">
    <input type="submit" value="Enable level 1">
</form>
1

There are 1 answers

1
CyberHavenProgramming On BEST ANSWER

I FIGURED IT OUT:

All I had to do was change my url patterns into the following order:

path('', views.home, name='home'),
path('check_tier_level/', views.check_tier_level, name='check_tier_level'),
path('check_tier_level/gain_access/', views.gain_access, name='gain_access'),
path('<str:special_message>/', views.home, name='home_special_message'),

The only difference here and what I have below, is the position of the 2nd function that goes to home. I'm going to leave this question up in case someone else comes across this same problem. I don't know why this made it work, but it did. Now everything works perfectly.