class backends.base.SessionBase
This is the base class for all session objects. It has the following standard dictionary methods:
-
__getitem__(key)
-
Example:
fav_color = request.session['fav_color']
-
__setitem__(key, value)
-
Example:
request.session['fav_color'] = 'blue'
-
__delitem__(key)
-
Example:
del request.session['fav_color']
. This raisesKeyError
if the givenkey
isn’t already in the session.
-
__contains__(key)
-
Example:
'fav_color' in request.session
-
get(key, default=None)
-
Example:
fav_color = request.session.get('fav_color', 'red')
-
pop(key, default=__not_given)
-
Example:
fav_color = request.session.pop('fav_color', 'blue')
-
keys()
-
items()
-
setdefault()
-
clear()
It also has these methods:
-
flush()
-
Deletes the current session data from the session and deletes the session cookie. This is used if you want to ensure that the previous session data can’t be accessed again from the user’s browser (for example, the
django.contrib.auth.logout()
function calls it).
-
set_test_cookie()
-
Sets a test cookie to determine whether the user’s browser supports cookies. Due to the way cookies work, you won’t be able to test this until the user’s next page request. See Setting test cookies below for more information.
-
test_cookie_worked()
-
Returns either
True
orFalse
, depending on whether the user’s browser accepted the test cookie. Due to the way cookies work, you’ll have to callset_test_cookie()
on a previous, separate page request. See Setting test cookies below for more information.
-
delete_test_cookie()
-
Deletes the test cookie. Use this to clean up after yourself.
-
set_expiry(value)
-
Sets the expiration time for the session. You can pass a number of different values:
- If
value
is an integer, the session will expire after that many seconds of inactivity. For example, callingrequest.session.set_expiry(300)
would make the session expire in 5 minutes. - If
value
is adatetime
ortimedelta
object, the session will expire at that specific date/time. Note thatdatetime
andtimedelta
values are only serializable if you are using thePickleSerializer
. - If
value
is0
, the user’s session cookie will expire when the user’s Web browser is closed. - If
value
isNone
, the session reverts to using the global session expiry policy.
Reading a session is not considered activity for expiration purposes. Session expiration is computed from the last time the session was modified.
- If
-
get_expiry_age()
-
Returns the number of seconds until this session expires. For sessions with no custom expiration (or those set to expire at browser close), this will equal
SESSION_COOKIE_AGE
.This function accepts two optional keyword arguments:
-
get_expiry_date()
-
Returns the date this session will expire. For sessions with no custom expiration (or those set to expire at browser close), this will equal the date
SESSION_COOKIE_AGE
seconds from now.This function accepts the same keyword arguments as
get_expiry_age()
.
-
get_expire_at_browser_close()
-
Returns either
True
orFalse
, depending on whether the user’s session cookie will expire when the user’s Web browser is closed.
-
clear_expired()
-
Removes expired sessions from the session store. This class method is called by
clearsessions
.
-
cycle_key()
-
Creates a new session key while retaining the current session data.
django.contrib.auth.login()
calls this method to mitigate against session fixation.
Please login to continue.