简体   繁体   English

django每个用户一个会话

[英]django one session per user

based on docs ( http://docs.djangoproject.com/en/1.1/topics/http/sessions/ ) (yes - 1.1) Django creates unique sessions to all users. 基于文档( http://docs.djangoproject.com/en/1.1/topics/http/sessions/ )(是-1.1)Django为所有用户创建了唯一会话。 Logged user contains _auth_user_id . 登录的用户包含_auth_user_id How can i achieve such check in login: 我如何在登录中实现这种检查:

If new_login._auth_user_id in database:
   delete(sessions_containing_same_id_except_new_one)

The main idea is to allow only one session per user and delete old sessions. 主要思想是每个用户仅允许一个会话并删除旧会话。

UPDATE: The idea right now is to save sessionid while logging and if sessionid changes delete old entry before replacing. 更新:现在的想法是在登录时保存sessionid,如果sessionid更改,则在替换之前删除旧条目。 ATM missing part is to get that session id. ATM缺少的部分是获取该会话ID。

UPDATE: I got the sessionid with request.session.session_key . 更新:我得到了具有request.session.session_key的sessionid。 The problem was that sessionid is created after login. 问题是登录创建了sessionid。 If you request key before it was created - it creates new one instead of giving any warning. 如果您在创建密钥之前要求它-它会创建一个新密钥而不发出任何警告。

I created extra field for user (userattributes extends user): 我为用户创建了额外的字段(userattributes扩展了用户):

class UserAttributes(User):
    last_session_key = models.CharField(blank=True, null=True, max_length=40)

and method: 和方法:

def set_session_key(self, key):
    if self.last_session_key and not self.last_session_key == key:
        Session.objects.get(session_key=self.last_session_key).delete()
    self.last_session_key = key
    self.save()  

and i called it just after login: 登录后我就叫它:

auth.login(request, user)
user.userattributes.set_session_key(request.session.session_key)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM