Django user sessions, cookies and timeout

I am working with a Django application, and my current goal is to track a user's session using cookies. I have a feeling that, as always, my understanding is a little embarrassing about how I do it.

To begin with, I would like to control how much time has passed since the user entered the system, so I can successfully withdraw them if they have not visited a new page in the "x" hours. I'm not sure what exactly is standard (for a social network).

Is this information on my server? Do cookies really have any relevance here? I used cookies before to store things like user timezone, but I'm struggling to figure out how I track the user.

All I currently have in terms of user end is the django.contrib.auth package.

The only thing I really know how to do in terms of “capturing” user information is done using operators such as if request.user.is_authenticated(): (etc.).

I understand this is a tricky question, so I will try to narrow it down:

How can I expand existing information about the current user to capture the "last activity" so that I can log him out if they have not used the site for a certain period of time? Do I need to define a custom model?

The next step after this is to create a different type of user, so it seems to me that I need to create custom models - in addition to expanding the regular user form to create a profile, etc.

Thank you for understanding,

I know that I can be confusing when I don't understand things.

Thank you for your time,

James

+4
source share
1 answer

You can configure session middleware to automatically retrieve the user, set SESSION_COOKIE_AGE to some low value and set SESSION_SAVE_EVERY_REQUEST as True .

This will lead to automatic user logout after a certain inactivity, without the need to expand the profile.

  SESSION_COOKIE_AGE Default: 1209600 (2 weeks, in seconds) >> The age of session cookies, in seconds. SESSION_SAVE_EVERY_REQUEST Default: False >> Whether to save the session data on every request. If this is False (default), then the session data will only be saved if it has been modified – that is, if any of its dictionary values have been assigned or deleted. 

And to create a user / extensible User profile, Django 1.5 comes with a custom user model , please check the docs .

+7
source

Source: https://habr.com/ru/post/1492882/


All Articles