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

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

问题描述

我以前问了一个类似的问题,但我已经做了更多的研究,这个迭代应该有所不同。好像有几个用户在一个视图中注册和登录用户有问题,并没有真正得到回答。



问题是我在单个Django视图中注册,验证和登录用户。对于大多数用户而言,对于其他用户而言,他们的后续请求(他们点击我网站上的一个链接)返回匿名用户。不知何故,登录的用户丢失了会话,并被重定向到我的站点etht不需要身份验证的页面。



当他们通过纯登录视图(而不是注册+登录视图)登录时,会话数据保持不变。这个问题真的是在一个视图中注册和登录。



看到同一个问题的这篇文章: https://stackoverflow.com/questions/1693726/problem-with-combined-authentication-login-view



有人建议这可能是一个线程问题。我也看到它建议它涉及缓存会话数据的后端。任何关于它真正涉及到的内容的想法?/ p>

我无法复制错误,这真的让我回来了。



编辑 - 我应该注意,我使用默认数据库支持的会话。



这是我的注册/登录视图

  def splash_register )
如果request.session.get('beta'):

如果request.method =='POST':
userform = MyUserCreationForm(request.POST)
如果userform.is_valid():
#username为< 30 char是Django用户模型所必需的。我将用户名作为用户电子邮件的哈希存储

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



username = user.username
password = str(userform.cleaned_data ['password'])
user = auth.authenticate(
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))
返回render_to_response(website / splash_register.html,context_instance = RequestConte xt(request))
else:
return HttpResponseRedirect('/ splash /')


解决方案

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



所以这样的工作:

  def splash_register(request):
如果request.session.get('beta'):

如果request.method =='POST':
userform = MyUserCreationForm(request.POST)
如果userform.is_valid():
Django用户模型需要< 30 char的#username。我将用户名作为用户电子邮件的哈希存储

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 /')



更新



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

  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: https://stackoverflow.com/questions/1693726/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天全站免登陆