Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python

Rajesh Kumar
Rajesh Kumar
12,733 Points

Is it possible to redirect to the same page after logging in after session expiry. Please read the description.. Thanks.

Django v1.9, Python 2.7: The doubt/challenge here is if the web application does not have the Session expiry time set.

Can we write a logic to store the URL which the user is viewing and the user is logged out due to longer time of inactivity and redirected to the same page after logging in.

I hope my question is clear.

Thanks, Rajesh

Rajesh Kumar
Rajesh Kumar
12,733 Points

Hi Alexey Kislitsin,

Yeah, I thought about accessing request.path(in Login view) when the user logs in which would eventually redirect to the url stored in previous session. But when the log out view is invoked request.session.flush()...flushes out all the information stored in session hence that does not give the option to access the path. Is there a way to avoid commenting out request.session.flush() or is there any other way.....

Appreciate your inputs Alexey :).

Thanks in advance. Rajesh...

Alx Ki
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Alx Ki
Python Web Development Techdegree Graduate 14,822 Points

Rajesh Kumar yes, but what if you save info from request path to user's profile? I mean:

models.py

from django.db import models
from django.contrib.auth.models import User
from django.db.models.signals import post_save
from django.dispatch import receiver

from django_countries.fields import CountryField

class Profile(models.Model):
    """User profile model. Uses OneToOneField
       to connect each user with a personal profile."""
    user = models.OneToOneField(User, on_delete=models.CASCADE)
    last_url = models.URLField() ### What if you assign request.path with this right before logout()



#  Defining SIGNALS to create profiles at once with users:
#  hooking create_user_profile and save_user_profile to User model
#  it should automatically create/update Profile while User created/updated.


@receiver(post_save, sender=User)
def create_user_profile(sender, instance, created, **kwargs):
    if created:
        Profile.objects.create(user=instance)


@receiver(post_save, sender=User)
def save_user_profile(sender, instance, **kwargs):
    instance.profile.save()

Each user will have own last_url saved before logout. After next login you can use it to redirect. I didn't try it.. But I guess It should work.

If you find a solution, please tell me.

1 Answer

Rajesh Kumar
Rajesh Kumar
12,733 Points

Hey Alexey Kislitsin, thank you so much for the input. Will try that for sure and let you know...

Thanks, Rajesh