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
19
votes
6 answers

How to clear all session variables without getting logged out

I am trying to clear all of the session variables but not logout the current user. user = request.session.get('member_id', None) request.session.flush() request.session.modified = True request.session['member_id'] = user request.session.modified =…
Siecje
  • 3,594
  • 10
  • 32
  • 50
18
votes
3 answers

Testing a session variable

I came across Django request.session; I know how to set and test it for a specific value. request.session['name'] = "dummy" and somewhere I check if request.session['name'] == "dummy" : #do something But, now I have to check whether the session…
user739811
18
votes
3 answers

Django session expiry?

From django's documentation, I became under the impression that calling: request.session.set_expiry(300) from one view would cause the session to expire after five minutes inactivity; however, this is not the behavior that I'm experiencing in…
slypete
  • 5,538
  • 11
  • 47
  • 64
17
votes
2 answers

When django session is created

I don't really understand when session is created and per what entity it is created (per ip, per browser, per logged in user). I see in documentation that sessions by default is created per visitor - but what is visitor (browser or ip)?
sunprophit
  • 1,639
  • 4
  • 16
  • 39
16
votes
3 answers

How to delete nested session data in Django?

I set data to session with 'cart' key: request.session['cart'] = {'8': ['a', 'b'], '9': ['c', 'd']} Then, I could delete all the session data with 'cart' key: del request.session['cart'] But, I could not delete ['a', 'b'] with '8' key: del…
ezdookie
  • 1,477
  • 3
  • 15
  • 19
14
votes
4 answers

Django: any way to avoid querying for request.user on every request?

For my website pretty much every page has a header bar displaying "Welcome, ABC" where "ABC" is the username. That means request.user will be called for every single request resulting in database hits over and over again. But once a user is logged…
Continuation
  • 12,722
  • 20
  • 82
  • 106
14
votes
2 answers

Django sessions for anonymous users

I want to be able to collect basic stats on the use of a webapp by users, both anonymous and logged-in. The commonality here would be that using session ids, I could store data for both logged-in and logged-out users, and still be able to link the…
jvc26
  • 6,363
  • 6
  • 46
  • 75
13
votes
3 answers

How to limit number of concurrent users logging in to same account in Django

My site is a digital marketplace website written in Django. Digital content(text, images, videos) on the site is 'locked' by default. Only users who bought those content can view it. There's a story that certain user(who bought the content) give…
13
votes
6 answers

How to check whether a user is online in django template?

In template, when I use {% if topic.creator.is_authenticated %} Online {% else %} Offline {% endif %} the users turn out to be always online, even when they has signed out a while ago. So I'm wondering how to check the online users correctly?
Jand
  • 2,527
  • 12
  • 36
  • 66
13
votes
1 answer

How to capture the session key for an anonymous user (django 1.6)

Am trying to keep track of AnonymousUsers by their session information (if possible). In older versions of Django, I could do something like: def my_view(request): # in case the user wasn't logged in, create/save a session if not…
thornomad
  • 6,707
  • 10
  • 53
  • 78
12
votes
2 answers

django Sessions are not maintaing in an iframe

I am creating a conversational chatbot using django . And To maintain the flow of the chat in chatbot , i am using django sessions . But when i use the link of the chatbot in an iframe , it doesn't store any of the session and flow breaks down. I…
kd007
  • 339
  • 1
  • 13
12
votes
1 answer

Performance comparison of using django signed cookie session over django db + cache based session?

Django 1.4 offers several ways to maintain django sessions : My viewpoint on using : i) cache only : Not preferable, users sessions may get purged out of memcache. ii) Db + cache (cached_db): Preferable , simple and secure solution. iii) Signed…
DhruvPathak
  • 42,059
  • 16
  • 116
  • 175
10
votes
4 answers

Where does Django store sessions?

I have this code in my project that assigns a unique id to an anomymous user and saves it in a session: user_id = str(uuid.uuid4())[:5] request.session['nonuserid'] = user_id Documentation says that sessions are stored in my database. I thought it…
Jahongir Rahmonov
  • 13,083
  • 10
  • 47
  • 91
9
votes
2 answers

Django user sessions with AWS Load Balancer stickiness turned off

I'm using AWS Elastic Beanstalk with EC2 servers behind an Elastic Load Balancer (ELB). I have "sticky sessions" on the ELB enabled because that's the only way I can get django user sessions to work correctly. However, during times of peak traffic,…
9
votes
4 answers

AttributeError: 'WSGIRequest' object has no attribute 'session'

I keep getting this error at random times and whenever I touch the django.wsgi file, it gets fixed only to happen again after a few hours. I'm lost as to what to do. my middleware_classes is as follows: MIDDLEWARE_CLASSES = ( …
1
2
3
32 33