会话数据库表清理
该表是否需要清除或者由 Django 自动处理?
Does this table need to be purged or is it taken care of automatically by Django?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
该表是否需要清除或者由 Django 自动处理?
Does this table need to be purged or is it taken care of automatically by Django?
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(6)
Django 不提供自动清除功能。然而,有一个方便的命令可以帮助您手动完成此操作: Django 文档:清除会话存储
Django does NOT provide automatic purging. There is however a handy command available to help you do it manually: Django docs: Clearing the session store
cornJob
cornJob
在我的
开发服务器
上,我更喜欢数据库命令而不是python manage.pyclearsessions
,因为您删除所有会话,而不仅仅是过期的会话(此处:MySQL)。要登录数据库并执行以下操作:顺便说一句,
session
不是数据库,而是一个表 (django_session) 和一个应用程序 (django.contrib.sessions
)。On my
development server
, I prefer a database command overpython manage.py clearsessions
because you delete all sessions, not just the expired ones (here: MySQL). To login into your database and do:BTW,
session
is not a database, but a table (django_session) and an app (django.contrib.sessions
).不要执行
Session.objects.all().delete()
或TRUNCATE TABLE django_session;
,因为这会删除所有会话,包括活动会话那些!如果您想以编程方式清除过期会话:
Do NOT execute
Session.objects.all().delete()
norTRUNCATE TABLE django_session;
, as that would delete all sessions, including active ones!If you want to clear expired sessions programmatically:
我知道这篇文章很旧,但我尝试了这个命令/属性,它对我有用。
在“base.html”文件中,我插入了:
{{ request.session.clear_expired }}
当用户在会话过期后单击模板中的任何链接时,这将从 django_session 表中清除过期记录。
即便如此,还是有必要创建一个例程来清除超过一天的过期记录。当用户关闭打开会话的浏览器时,这是清除日志所必需的。
我用的是 Django 3.2.4
I know this post is old but I tried this command/attribute and it worked for me.
In the 'base.html' file, I inserted:
{{ request.session.clear_expired }}
This clears expired records from the django_session table when the user clicks on any link in the template after the session expires.
Even so, it is necessary to create a routine to clear expired records over a period longer than one day. This is necessary to clear logs when user closes browser with open sessions.
I used Django 3.2.4
其他方法:
我使用的是 Django 3.2,我建议使用 django-auto-logout 包。
它允许活动时间和空闲时间会话控制。
在模板中,您可以将变量与 Javascript 一起使用。
Other method:
I'm using Django 3.2 and i recommend using the django-auto-logout package.
It allows active time and idle time session control.
In the template you can use variables together with Javascript.