Django 自动注销未在数据库中更新
Django auto logout not updating in Database
我正在使用 Python(2.7) 和 Django(1.11) 开发一个项目,我需要在几分钟或几小时内实现自动注销。
Note: I have searched a lot and take a look on a various related question but couldn't find any solution for my specific problem, so don't mark it as duplicate, please!
我通过在 settings.py
中添加一些设置实现了这一点:
SESSION_COOKIE_AGE = 120
SESSION_SAVE_EVERY_REQUEST = True
LOGOUT_REDIRECT_URL = 'mainlogin'
只是为了测试,我设置了2分钟。
2 分钟后,当我刷新页面时,用户会注销并重定向到登录页面,但在 backend/database 中,用户的状态仍然是 active
.
我需要进一步使用活动用户 processing/execution,因此如果用户自动注销,则活动用户不应该可用,但它是。
如果我手动注销用户,它不再显示在活动用户中,这意味着手动注销运行良好,但自动注销仍将用户显示为 active
用户。
以下是我获得活跃用户的方法:
all_active_users = user_table.objects.filter(user_type='a', status=1, approval_status=1, is_active=True)
# further execution
Update: I'm using the custom mode for user which is named as user_table
and here's what I have tried as suggested by the an answer:
def get_all_logged_in_users():
# Query all non-expired sessions
# use timezone.now() instead of datetime.now() in latest versions of Django
sessions = Session.objects.filter(expire_date__gte=timezone.now())
uid_list = []
# Build a list of user ids from that query
for session in sessions:
data = session.get_decoded()
uid_list.append(data.get('id', None))
# Query all logged in users based on id list
return user_table.objects.filter(id__in=uid_list)
它 returns 空查询集为: active drivers are: <QuerySet []>
即使 driver
类型的用户也已登录。
如何解决这个问题,当用户自动注销时,它不会显示在 active
个用户中。
您误解了 is_active
的含义。 Django 使用此字段来确定用户是否允许 登录,而不是他们是否恰好在那个时刻登录。
即使您想创建自己的 User
字段来存储此信息,也很难做到,因为会话会随着时间的流逝而过期。服务器上实际上没有发生任何可能触发模型字段更改的事情。
因此,如果您需要确定所有登录用户,您需要使用会话存储。 Django 没有提供一种直接的方法来做到这一点,但它确实是可能的。 This answer 显示了如何查询 Session
table 以确定谁已登录。
我正在使用 Python(2.7) 和 Django(1.11) 开发一个项目,我需要在几分钟或几小时内实现自动注销。
Note: I have searched a lot and take a look on a various related question but couldn't find any solution for my specific problem, so don't mark it as duplicate, please!
我通过在 settings.py
中添加一些设置实现了这一点:
SESSION_COOKIE_AGE = 120
SESSION_SAVE_EVERY_REQUEST = True
LOGOUT_REDIRECT_URL = 'mainlogin'
只是为了测试,我设置了2分钟。
2 分钟后,当我刷新页面时,用户会注销并重定向到登录页面,但在 backend/database 中,用户的状态仍然是 active
.
我需要进一步使用活动用户 processing/execution,因此如果用户自动注销,则活动用户不应该可用,但它是。
如果我手动注销用户,它不再显示在活动用户中,这意味着手动注销运行良好,但自动注销仍将用户显示为 active
用户。
以下是我获得活跃用户的方法:
all_active_users = user_table.objects.filter(user_type='a', status=1, approval_status=1, is_active=True)
# further execution
Update: I'm using the custom mode for user which is named as
user_table
and here's what I have tried as suggested by the an answer:
def get_all_logged_in_users():
# Query all non-expired sessions
# use timezone.now() instead of datetime.now() in latest versions of Django
sessions = Session.objects.filter(expire_date__gte=timezone.now())
uid_list = []
# Build a list of user ids from that query
for session in sessions:
data = session.get_decoded()
uid_list.append(data.get('id', None))
# Query all logged in users based on id list
return user_table.objects.filter(id__in=uid_list)
它 returns 空查询集为: active drivers are: <QuerySet []>
即使 driver
类型的用户也已登录。
如何解决这个问题,当用户自动注销时,它不会显示在 active
个用户中。
您误解了 is_active
的含义。 Django 使用此字段来确定用户是否允许 登录,而不是他们是否恰好在那个时刻登录。
即使您想创建自己的 User
字段来存储此信息,也很难做到,因为会话会随着时间的流逝而过期。服务器上实际上没有发生任何可能触发模型字段更改的事情。
因此,如果您需要确定所有登录用户,您需要使用会话存储。 Django 没有提供一种直接的方法来做到这一点,但它确实是可能的。 This answer 显示了如何查询 Session
table 以确定谁已登录。