Monday, February 22, 2021

Why is this acting like a permanent redirect?

I have a Django view that I redirect a user to after they login:

# settings.py
LOGIN_REDIRECT_URL = 'login-redirect'

# account/urls.py
        urlpatterns = [
            path('', include('django.contrib.auth.urls')),
            ...
        ]

# home/views.py
from account.models import Member

def login_redirect(request):
"""
Redirect member to appropriate page when they log in.
"""
member = Member.objects.get(user__id__exact=request.user.id)
if member.has_profile:
return redirect('homepage', member_id=member.id)
else:
return redirect('profile-create', member_id=member.id)

If I start Django's development server and execute this code when the condition ```member.has_profile = True```, Django redirects me to the homepage view as expected.  But if I then go into the database and set has_profile to False, restart the web server, and execute code again, my website acts as if the code isn't executed.  Instead, I'm sent immediately to the homepage view again.  I can see this by looking at the terminal console:

    [19/Feb/2021 ...] "GET /account/login/ HTTP/1.0" 200 4268       # I'm using Django's default login method
    [19/Feb/2021 ...] "POST /account/login HTTP/1.0" 302 0
    [19/Feb/2021 ...] "GET /home/login_redirect/ HTTP/1.0" 200 4599

This happens even though I am restarting the server and clearing my browser's cache.  In fact, I can edit the login_redirect function so that it only contains a single line ```pass``` and I'm still sent to the homepage after logging in.

def login_redirect(request):
pass

Why is this happening?  I'm not doing a permanent redirect.  And should I not be using the redirect method in my login_redirect method to send a user to a different page depending on whether or not they have a profile?

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/8423cc0d-b83b-4115-b9fa-6a28e93210d8n%40googlegroups.com.

No comments:

Post a Comment