django-sessions

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

若如初见. 提交于 2019-12-02 22:45:19
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 time a user logs in from a different location (different computer/browser), a new Session() is created and saved with a unique session_id . This can result in multiple database entries for the same user. Their login persists on that node until the cookie is deleted or session expires. When a user changes their password, I want to delete all unexpired sessions for that user from

How do I modify the session in the Django test framework

北城余情 提交于 2019-11-30 06:32:24
问题 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 like to do this: from django.contrib.sessions.models import Session s = Session() s.expire_date = '2010-12-05' s.session_key = 'my_session_key' s.save() self.client.session = s response = self.client.get('/myview/') But I get the error:

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

谁都会走 提交于 2019-11-30 03:42:21
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 = models.PositiveIntegerField() ip_address = models.IPAddressField() date_last_visited = models.DateTimeField() def __unicode__(self): return u'(%s, %s)' % (str(self.session.session_key), str(self.page.name)) To make a new entry for this model I'm using the following to get the current session (in views.py): Session.objects.get(session_key=request.session.session_key) However

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

你说的曾经没有我的故事 提交于 2019-11-29 00:48:09
问题 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 = models.PositiveIntegerField() ip_address = models.IPAddressField() date_last_visited = models.DateTimeField() def __unicode__(self): return u'(%s, %s)' % (str(self.session.session_key), str(self.page.name)) To make a new entry for this model I'm using the following to get

How do I modify the session in the Django test framework

风流意气都作罢 提交于 2019-11-28 19:12:09
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 like to do this: from django.contrib.sessions.models import Session s = Session() s.expire_date = '2010-12-05' s.session_key = 'my_session_key' s.save() self.client.session = s response = self.client.get('/myview/') But I get the error: AttributeError: can't set attribute Thoughts on how to modify the client session before making get requests? I

Delete session key from all users

佐手、 提交于 2019-11-28 11:11:16
When a user logs in some details are saved to the session, lets say using key='user_settings' When some updates happens within the system I need to loop through all users and delete the 'user_settings' key from each users session. That way I will trigger loading the details fresh from the database. How can I loop through all sessions and remove the key from each of the sessions? Looping Even if your site has only a few thousand users, you might find that the django_sessions table has a few million entries in it. This happens if the sessions haven't been cleaned up. If you loop through them and

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

柔情痞子 提交于 2019-11-27 21:44:37
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 ‌Thanks this excellent blog post, with slight modifications, I came up with a better solution, which uses memcache, hence less delay per request: in models.py add: from django.core.cache import cache import datetime from myproject import settings and add these method the userprofile class: def last_seen(self): return cache.get('seen_%s' % self.user

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

痴心易碎 提交于 2019-11-27 04:32:16
问题 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? 回答1: ‌Thanks this excellent blog post, with slight modifications, I came up with a better solution, which uses memcache, hence less delay per request: in models.py add: from django.core.cache import cache import datetime from myproject import settings

Django, SESSION_COOKIE_DOMAIN with multiple domains

人走茶凉 提交于 2019-11-26 13:44:45
问题 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 both domains to login? 回答1: If you set your session cookie domain to start with a "." character it will let you handle wildcard sub-domains and share a session cookie (login session) across multiple subdomains. In settings.py: SESSION_COOKIE_DOMAIN="