如何查找django的会话为一个特定的用户? [英] How to lookup django session for a particular user?

查看:256
本文介绍了如何查找django的会话为一个特定的用户?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写,我会从Django和从独立的应用程序访问数据库的应用程序。两者都需要做会话验证和会话应该是他们两个人是一样的。 Django的有一个内置的验证/会话验证,这是我在用的,现在我需要弄清楚如何重复使用相同的会话我的独立应用程序。

I am writing an application where I will be accessing the database from django and from a stand alone application. Both need to do session verification and the session should be the same for both of them. Django has a built in authentication/session verification, which is what I am using, now I need to figure out how to reuse the same session for my stand alone application.

我的问题是我怎么能查找一个session_key可以为特定的用户?

My question is how can I look up a session_key for a particular user?

这是它看起来没有什么联系在一起AUTH_USER和django_session中

From what it looks there is nothing that ties together auth_user and django_session

推荐答案

修改 django_session中表中添加一个明确的 USER_ID 可以让生活变得更加简单。假设你做(或类似的东西),这里是改写(munging)的东西根据自己的喜好四种方法:

Modifying the django_session table to add an explicit user_id can make life a lot easier. Assuming you do that (or something similar), here are four approaches to munging things to your liking:

叉子 django.contrib.session code。我知道,我知道,这是一个可怕的建议。但它只有500线,包括所有的后端和减去测试。这是pretty简单的破解。这只是如果你准备做事情一些严重清理的最佳路线。

Fork the django.contrib.session code. I know, I know, that's a horrible thing to suggest. But it's only 500 lines including all backends and minus the tests. It's pretty straightforward to hack. This is the best route only if you are going to do some serious rearranging of things.

如果您不想叉,你可以尝试连接到 Session.post_save 信号和munge那里。

If you don't want to fork, you could try connecting to the Session.post_save signal and munge there.

或者你可以猴补丁 contrib.session.models.Session.save()。只是包装现有的方法(或创建一个新的),突破/合成任何你需要的值,并将其存储在您的新域,然后超(会话,个体经营).save()

Or you could MonkeyPatch contrib.session.models.Session.save(). Just wrap the existing method (or create a new one), breakout/synthesize whatever values you need, store them in your new fields, and then super(Session, self).save().

然而,这样做的另一种方式是把2(是,二)中间件类 - 一前一后的 SessionMiddleware 在你的 settings.py 的文件。这是因为中间件被处理的方式。在 SessionMiddleware 中列出的人会得到的,在入站请求,与会话的请求已经连接到它。上市前可以做任何处理的响应和/或变更/重新保存会话。

Yet another way of doing this is to put in 2 (yes, two) middleware classes -- one before and one after SessionMiddleware in your settings.py file. This is because of the way middleware is processed. The one listed after SessionMiddleware will get, on the inbound request, a request with the session already attached to it. The one listed before can do any processing on the response and/or change/resave the session.

我们用这最后的技术变化来创建搜索引擎蜘蛛伪会议给他们的会员制通常是材料的特殊访问。我们也检测内部链接,其中 REFERER 字段是从相关的搜索引擎,我们给用户完全访问的一篇文章。

We used a variation on this last technique to create pseudo-sessions for search engine spiders to give them special access to material that is normally member-only. We also detect inbound links where the REFERER field is from the associated search engine and we give the user full access to that one article.

更新:

我的回答是现在很古老,但它仍然是大部分正确的。见@ Gavin_Ballard的下面更近的答案(2014年9月29日)为另一种方法解决这个问题。

My answer is now quite ancient, although it still is mostly correct. See @Gavin_Ballard's much more recent answer (9/29/2014) below for yet another approach to this problem.

这篇关于如何查找django的会话为一个特定的用户?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆