Why is this acting like a permanent redirect?

29 views
Skip to first unread message

Robert F

unread,
Feb 22, 2021, 3:08:08 PM2/22/21
to Django users
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?

David Nugent

unread,
Feb 22, 2021, 7:56:11 PM2/22/21
to django...@googlegroups.com, Robert F
Hi Robert,

To point out the most likely cause of the issue: LOGIN_REDIRECT_URL is a url, not a route name.

Possibly could use a reverse_lazy('login-redirect') here after import django.urls, but - untested.

Regards,
David
--
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...@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.

Robert F

unread,
Feb 23, 2021, 10:41:15 AM2/23/21
to Django users
Thanks David.  I'll give this a try.
Reply all
Reply to author
Forward
0 new messages