Django,如何在管理界面中查看会话数据
我正在使用 Django 会话,我想要一种查看会话数据的方法管理界面。这可能吗?
即,对于每个会话,我想查看存储在会话数据库中的数据(据我所知,它本质上是一本字典)。
目前我只能在 Session data
字段中看到哈希,例如:
gAJ9cQEoVQ5zb3J0aW5nX2Nob2ljZXECVQJQT3EDVQxnYW1lc19wbGF5ZWRxBH1xBVgLAAAAcG9z
dG1hbi1wYXRxBksDc1UKaXBfYWRkcmVzc3EHVQkxMjcuMC4wLjFxCFUKdGVzdGNvb2tpZXEJVQZ3
b3JrZWRxClUKZ2FtZV92b3Rlc3ELfXEMdS4wOGJlMDY3YWI0ZmU0ODBmOGZlOTczZTUwYmYwYjE5
OA==
我已将以下内容放入 admin.py 中以实现此目的:
from django.contrib.sessions.models import Session
...
admin.site.register(Session)
特别是我希望能够至少查看每个会话的 IP 地址。 (如果我可以计算每个 IP 地址有多少个会话,并根据每个 IP 地址的总会话数对 IP 进行排序,那就太好了。)
感谢您的帮助:-)
I'm using Django sessions and I would like a way of seeing the session data in the admin interface. Is this possible?
I.e. for each session I want to see the data stored in the session database (which is essentially a dictionary as far as I can gather).
Currently I can just see a hash in the Session data
field, such as:
gAJ9cQEoVQ5zb3J0aW5nX2Nob2ljZXECVQJQT3EDVQxnYW1lc19wbGF5ZWRxBH1xBVgLAAAAcG9z
dG1hbi1wYXRxBksDc1UKaXBfYWRkcmVzc3EHVQkxMjcuMC4wLjFxCFUKdGVzdGNvb2tpZXEJVQZ3
b3JrZWRxClUKZ2FtZV92b3Rlc3ELfXEMdS4wOGJlMDY3YWI0ZmU0ODBmOGZlOTczZTUwYmYwYjE5
OA==
I have put the following into admin.py to achieve this:
from django.contrib.sessions.models import Session
...
admin.site.register(Session)
In particular I was hoping to be able to see at least an IP address for each session. (Would be nice too if I could count how many sessions per IP address and order the IPs based on number of sessions in total for each.)
Thank you for your help :-)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
你可以这样做:
甚至可能 get_decoded 可以直接在list_display中使用。如果存在一些问题导致其无法正常工作,您可以根据链接的 Django 源代码自行解码会话数据。
You can do something like this:
It might be even that get_decoded can be used directly in list_display. And in case there's some catch that prevents this from working ok, you can decode the session data yourself, based on the linked Django source.
继续托马斯的回答,我继续说:
Continuing from Tomasz's answer, I went with:
会话数据包含在 base64 编码的 pickled 字典中。这就是您在管理中看到的内容,因为该数据存储在会话模型中的 TextField 中。
我认为任何分布式 Django 代码都不会在会话中存储 IP 地址,但如果您可以访问它,您可以自己执行此操作。
为了显示真实的会话信息,您可以编写自己的表单字段来显示解码的信息。请记住,如果您想修改 save 方法,则还必须覆盖它。您可以在 django/contrib/sessions/models.py 中查看编码和解码方法。
Session data is contained in a base64 encoded pickled dictionary. That's is what you're seeing in the admin because that data is stored in a TextField in the Session model.
I don't think any distributed Django code stores the IP address in the session but you could do it yourself if you can access it.
In order to display the real session information, you may write your own form field that presents the decoded information. Keep in mind that you'll have to also overwrite the save method if you want to modify it. You can take a look at the encode and decode methods in
django/contrib/sessions/models.py
.EB 的其他很好的答案给我留下了错误“数据库在 QuerySet.dates() 中返回了无效值。时区定义和 pytz 安装了吗?”。 (我确实安装了 db tz info 和 pytz,并且我的应用程序广泛使用时区。)删除“date_hierarchy”行为我解决了这个问题。所以:
EB's otherwise great answer left me with the error "Database returned an invalid value in QuerySet.dates(). Are time zone definitions and pytz installed?". (I do have db tz info and pytz installed, and my app uses timezones extensively.) Removing the 'date_hierarchy' line resolved the issue for me. So:
除了之前的答案之外,我们还可以向该会话的用户显示这有助于识别用户的会话。
Adding to previous answers, We can also show the user for that session which is helpful for identifying the session of users.