Tuesday, February 23, 2021

Re: Why is this acting like a permanent redirect?

Thanks David.  I'll give this a try.

On Monday, February 22, 2021 at 4:56:11 PM UTC-8 David Nugent wrote:
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

On 23 February 2021 at 07:07:52, Robert F (robe...@gmail.com) wrote:

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...@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.

--
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/d108d8e4-9ffa-4747-890c-ae5c3dc0367an%40googlegroups.com.

No comments:

Post a Comment