Django的1.2会话丢失(SO未成功回答了这个问题尚未) [英] Django 1.2 session loss (SO hasn't answered this question successfully yet)

查看:125
本文介绍了Django的1.2会话丢失(SO未成功回答了这个问题尚未)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我以前问过类似的问题,但我已经做了一些调查研究,这迭代应该是一个有点不同。它好像一些,使用户有与注册和在单个视图中的用户登录的问题,并没有真正得到回答。

问题是我注册,验证和登录在一个单一的Django视图的用户。对于大多数用户来说这很好,但对于其他用户,他们的后续请求(他们点击我的网站的链接)返回一个匿名用户。不知怎的,登录的用户失去了他们的会议,并重定向到一个页面上我坐ethat不需要身份验证。

当他们然后通过一个纯粹的登录视图登录(相对于寄存器+登录视图),会话数据留在机智。这个问题似乎真的被注册,并在一个单一的视图记录。

看到这个职位对同样的问题:<一href=\"http://stackoverflow.com/questions/1693726/problem-with-combined-authentication-login-view\">Problem与联合认证/登录视图。。

有人建议,这是一个潜在的线程问题。我也看到了它认为它涉及到后端缓存会话数据。

这是什么是真正涉及到有什么想法?我无法重现错误,这实在​​是拖我。

编辑 - 我要指出,我使用的是默认的数据库支持会话。

下面是我的注册/登录视图

 高清splash_register(要求):
  如果request.session.get('测试版'):    如果request.method =='POST':
        用户窗体= MyUserCreationForm(request.POST)
        如果userform.is_valid():
            &LT的#username 30 char是Django的用户模型所需。我存储的用户名作为用户电子邮件的哈希值            用户= userform.save(提交= FALSE)
            user.username =散列(user.email)
            user.save()            用户名= user.username
            密码= STR(userform.cleaned_data ['密码'])
            用户= auth.authenticate(用户名=用户名,密码=密码)
            如果用户不无:
                auth.login(请求,用户)
                的request.session ['first_visit'] = TRUE
                返回的Htt presponseRedirect(/)
            其他:
                返回的Htt presponseRedirect('/飞溅/注册/')
        其他:
            用户窗体= MyUserCreationForm(request.POST)
            返回render_to_response(网站/ splash_register.html,{窗体:用户窗体},context_instance = RequestContext的(要求))
    返回render_to_response(网站/ splash_register.html,context_instance = RequestContext的(要求))
其他:
    返回的Htt presponseRedirect('/飞溅/')


解决方案

您不必使用身份验证,在这种情况下,它是不是真的需要。所有你需要做的是设置用户记录的后端。

因此​​,像这样将工作:

 高清splash_register(要求):
  如果request.session.get('测试版'):    如果request.method =='POST':
        用户窗体= MyUserCreationForm(request.POST)
        如果userform.is_valid():
            &LT的#username 30 char是Django的用户模型所需。我存储的用户名作为用户电子邮件的哈希值            用户= userform.save(提交= FALSE)
            user.username =散列(user.email)
            user.backend ='django.contrib.auth.backends.ModelBackend
            user.save()
            用户名= user.username
            密码= STR(userform.cleaned_data ['密码'])
            auth.login(请求,用户)
            的request.session ['first_visit'] = TRUE
            返回的Htt presponseRedirect(/)
        其他:
            用户窗体= MyUserCreationForm(request.POST)
            返回render_to_response(网站/ splash_register.html,{窗体:用户窗体},context_instance = RequestContext的(要求))
    返回render_to_response(网站/ splash_register.html,context_instance = RequestContext的(要求))
其他:
    返回的Htt presponseRedirect('/飞溅/')

更新

我的评论中提到这一点,但在一个答案方面的解决办法是添加到您的设置文件:

  SESSION_COOKIE_DOMAIN ='yourdomain.com'

这将允许用户从www.yourdomain.com的的yourdomain.com登录到该网站。

进来

I've asked a similar question before, but I've done some more research and this iteration should be a bit different. It seems as though several SO users have had an issue with registering and logging in users in a single view and it hasn't really been answered.

The issue is that I register, authenticate, and login a user in a single Django view. For most users that's fine, but for other users, their subsequent request (they click a link on my site) returns an Anonymous User. Somehow, the logged in user loses their session and is redirected to a page on my sit ethat doesn't require authentication.

When they then log in via a pure login view (as opposed to the register + login view), the session data stays in tact. The issue really seems to be registering and logging in a single view.

See this post for the same issue: Problem with combined Authentication/Login view..

It has been suggested that this is potentially a threading issue. I've also seen it suggested that it relates to the backend for caching session data.

Any thoughts on what it really relates to? I can't reproduce the error, which is really holding me back.

EDIT--I should note that I'm using the default database backed sessions.

Here is my register/login view

def splash_register(request):
  if request.session.get('beta'):

    if request.method=='POST':
        userform=MyUserCreationForm(request.POST)
        if userform.is_valid():
            #username of <30 char is required by Django User model.  I'm storing username as a hash of user email 

            user=userform.save(commit=False)
            user.username=hash(user.email)
            user.save()



            username=user.username
            password=str(userform.cleaned_data['password'])
            user=auth.authenticate(username=username, password=password)
            if user is not None:
                auth.login(request,user)
                request.session['first_visit']=True
                return HttpResponseRedirect("/")
            else:
                return HttpResponseRedirect('/splash/register/')
        else:
            userform=MyUserCreationForm(request.POST)
            return render_to_response("website/splash_register.html", {'userform':userform}, context_instance=RequestContext(request))
    return render_to_response("website/splash_register.html", context_instance=RequestContext(request))     
else:
    return HttpResponseRedirect('/splash/')        

解决方案

You don't have to use authenticate and, in this scenario, it's not really needed. All you need to do is set the backend of the user record.

So something like this would work:

def splash_register(request):
  if request.session.get('beta'):

    if request.method=='POST':
        userform=MyUserCreationForm(request.POST)
        if userform.is_valid():
            #username of <30 char is required by Django User model.  I'm storing username as a hash of user email 

            user=userform.save(commit=False)
            user.username=hash(user.email)
            user.backend='django.contrib.auth.backends.ModelBackend'
            user.save()


            username=user.username
            password=str(userform.cleaned_data['password'])
            auth.login(request, user)
            request.session['first_visit']=True
            return HttpResponseRedirect("/")
        else:
            userform=MyUserCreationForm(request.POST)
            return render_to_response("website/splash_register.html", {'userform':userform}, context_instance=RequestContext(request))
    return render_to_response("website/splash_register.html", context_instance=RequestContext(request))     
else:
    return HttpResponseRedirect('/splash/')

Update

I mentioned this in a comment, but in terms of an "answer" the solution is to add this to your settings file:

SESSION_COOKIE_DOMAIN = 'yourdomain.com'

This will allow users coming in from www.yourdomain.com or yourdomain.com to log in to the website.

这篇关于Django的1.2会话丢失(SO未成功回答了这个问题尚未)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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