Questions tagged [django-sessions]

For questions related to Django's session mechanism.

Django provides full support for anonymous sessions. The session framework lets you store and retrieve arbitrary data on a per-site-visitor basis. It stores data on the server side and abstracts the sending and receiving of cookies. Cookies contain a session ID – not the data itself (unless you’re using the cookie based backend or a custom backend).

Reference: https://docs.djangoproject.com/en/stable/topics/http/sessions/

490 questions
107
votes
9 answers

How to find out the request.session sessionid and use it as a variable in Django?

I'm aware that you can get session variables using request.session['variable_name'], but there doesn't seem to be a way to grab the session id(key) as a variable in a similar way. Is this documented anywhere? I can't find it.
rmh
  • 4,806
  • 10
  • 33
  • 31
73
votes
3 answers

How to set and get session in Django?

I need to set a variable on session, when a user login happens. How can I do this? if request.user.is_authenticated(): profile = request.user.get_profile() request.session['idempresa'] = profile.idempresa My other question is in a…
fh_bash
  • 1,725
  • 4
  • 16
  • 23
72
votes
11 answers

How to expire Django session in 5minutes?

I'm using this to login the user in: def login_backend(request): if request.method == 'POST': username = request.POST['username'] password = request.POST['password'] user = authenticate(username=username,…
pynovice
  • 7,424
  • 25
  • 69
  • 109
46
votes
5 answers

Session database table cleanup

Does this table need to be purged or is it taken care of automatically by Django?
nemesisdesign
  • 8,159
  • 12
  • 58
  • 97
38
votes
5 answers

How do I modify the session in the Django test framework

My site allows individuals to contribute content in the absence of being logged in by creating a User based on the current session_key I would like to setup a test for my view, but it seems that it is not possible to modify the request.session: I'd…
Riley
  • 1,198
  • 1
  • 11
  • 9
32
votes
4 answers

Django, SESSION_COOKIE_DOMAIN with multiple domains

In Django, I have SESSION_COOKIE_DOMAIN set to my domain name. But I would actually like to run the same site with two different domain names. With SESSION_COOKIE_DOMAIN set, only the named domain allows the user to login. Is it possible to allow…
interstar
  • 26,048
  • 36
  • 112
  • 180
31
votes
5 answers

Logging users out of a Django site after N minutes of inactivity

I'm working on a website that requires us to log a user out after N minutes of inactivity. Are there any best practices for this using Django?
Brian Tol
  • 4,149
  • 6
  • 24
  • 27
27
votes
2 answers

How to display all session keys and values in Django?

How can I display all session keys and valuesv? I know that the value can be accessed with request.session['key'] but I want to know if there are other values that are set by others or set automatically during user logins or similar other events..
sherpaurgen
  • 3,028
  • 6
  • 32
  • 45
26
votes
2 answers

Django: setting a session and getting session key in same view

I want to store some things in a database, and am using the current session as a foreign key: from models.py class Visited(models.Model): session = models.ForeignKey(Session) page = models.ForeignKey(Page) times_visited =…
Jon Cox
  • 10,622
  • 22
  • 78
  • 123
25
votes
5 answers

Django, how to see session data in the admin interface

I'm using Django sessions and I would like a way of seeing the session data in the admin interface. Is this possible? I.e. for each session I want to see the data stored in the session database (which is essentially a dictionary as far as I can…
Jon Cox
  • 10,622
  • 22
  • 78
  • 123
24
votes
6 answers

Most optimized way to delete all sessions for a specific user in Django?

I'm running Django 1.3, using Sessions Middleware and Auth Middleware: # settings.py SESSION_ENGINE = django.contrib.sessions.backends.db # Persist sessions to DB SESSION_COOKIE_AGE = 1209600 # Cookies last 2 weeks Each…
Dave
  • 12,408
  • 12
  • 64
  • 67
23
votes
2 answers

Check if Session Key is set

I am attempting to create a relatively simple shopping cart in Django. I am storing the cart in request.session['cart']. Therefore, I'll need to access the data in this session when anything is added to it. However, if the session is not already…
Julio
  • 2,261
  • 4
  • 30
  • 56
22
votes
4 answers

Huge Django Session table, normal behaviour or bug?

Perhaps this is completely normal behaviour, but I feel like the django_session table is much larger than it should have to be. First of all, I run the following cleanup command daily so the size is not caused by expired sessions: DELETE FROM %s…
Wolph
  • 78,177
  • 11
  • 137
  • 148
22
votes
3 answers

How can I logout a user in Django?

Our Django deployment checks every night which active users can still be found in out LDAP directory. If they cannot be found anymore, we set them to inactive. If they try to login next time, this will fail. Here is our code that does this: def…
Torsten Bronger
  • 9,899
  • 7
  • 34
  • 41
19
votes
2 answers

Sometimes request.session.session_key is None

I hit a problem when get session_key from request.session. I am using Django1.8 and Python2.7.10 to set up a RESTful service. Here is snippet of my login view: user = authenticate(username=userName, password=passWord) if user is not None: # the…
Wesley
  • 1,857
  • 2
  • 16
  • 30
1
2 3
32 33