How to set the duration of a session within 4 minutes?

Suppose I do this:

request.session['x'] = 33

How to make this session variable expire after 4 minutes? Only this variable !! I do not want all sessions to expire.

If this is not possible, is there a function that can track it? (a function that itself uses sessions?)

+3
source share
3 answers

You will have to track the age of the session variable yourself. In real Python code, it might look something like this:

from datetime import datetime, timedelta

request.session['x'] = dict(dt=datetime.now(), value='something')

MAX_AGE = timedelta(seconds=240)

if ('x' in request.session and datetime.now() - request.session['x']['dt'] > MAX_AGE):
    del request.session['x']

It is also possible to store your value in a cookie depending on what data and its size.

+3
source

You cannot automatically expire a single variable in a session.

cookie, . . , cookie.

+1
request.session['x_settime'] = time x was set
if (request.session['x_settime'] is older than 4 minutes) {
     delete request.session['x']
     delete request.session['x_settime']
}

p.s. python

0

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


All Articles