Django会话在登录后间歇性地消失 [英] Django session intermittently disappears just after login

查看:211
本文介绍了Django会话在登录后间歇性地消失的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的django应用程序中,我以下列方式处理登录。用户进入网关页面(index.html) - 如果当前没有登录,那么将会有一个登录/密码以及其他资料。成功登录(或者如果他们在登录时还是去页面),页面呈现略有不同(不用登录表单)。

In my django app, I handle login in the following manner. Users go to a gateway page (index.html) - if they are not currently logged in, there will be a login/password form along with the other material. On successful login (or if they otherwise go to that page while logged in), the page is rendered slightly differently (sans login form).

我正在处理的方式在index.html的视图我做:

The way I am handling that is in the view for index.html I do:

logged_in = request.user.is_authenticated()

然后将 logged_in 变量传递给模板,该模板将被检查查看其呈现的页面版本。

and then the logged_in variable is passed to the template, which is checked to see which version of the page it renders.

当用户登录时,登录视图会调用:

When a user logs in, the login view calls:

user = authenticate(username=username, password=password)
if user is not none:
    login(request, user)

然后他们被重定向回到index.html。

And then they are redirected back to index.html.

不,这个工作完全正常。我看到的是,有时在 HttpResponseRedirect 和索引视图之间是request.user被清除。我已经记录了一段时间,写入日志作为登录视图中的最后一个项目和索引视图中的第一个项目。它对用户的影响是它看起来不正确地登录(除非没有消息告诉他们)。

More often than not, this works perfectly fine. What I see though is that sometimes between the HttpResponseRedirect and the index view is that request.user is wiped out. I have been logging this for a while now, writing to the log as the last item in the login view and first item in the index view. The effect it has for the user is that it looks like they incorrectly logged in (except w/ no message telling them that).

它似乎进来了,因为在系统中会很好一段时间,然后我会连续看到它发生在一个用户4-5次。我也应该注意到,除了在登录之外,我从来没有看到/听说过这种情况发生,据我所知(这可能发生了,没有人抱怨)一旦他们进来,他们就在。

It does seem to come in spurts, as in the system will be fine for a while, and then I'll see it happen to a user 4-5 times in a row. I should also note that I've never seen/heard of this happening at any point except at the login, as far as I can tell (it is possible that it has happened and no one has complained) once they're in, they're in.

我在这里做了一些明显错误的登录方法?

Am I doing something obviously wrong with my login methodology here?

推荐答案

Apache + Mod Python或WSGI使用线程,并可能预先缓存您的请求(取决于您的conf)。所以如果你改变了一些代码,你必须重新启动你的apache。那么这个问题应该会消失。

Apache+Mod Python or WSGI use threads and may pre cache your requests (depends on your conf). So if you changed something in code you have to restart your apache. then the problem should disapear.

这篇关于Django会话在登录后间歇性地消失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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