用户注册后Django自动登录(1.4) [英] Django automatic login after user registration (1.4)
本文介绍了用户注册后Django自动登录(1.4)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在成功注册用户时遇到问题 - 但是,我希望用户在注册时登录.这是代表我的注册视图的代码.关于为什么用户没有自动登录的任何想法?
I have an issue where I am successfully registering users - however, I want users to be logged in on registration. Here is the code that represents my registration view. Any thoughts on why the user is not auto-logged in?
注意事项:
- 用户正在正确注册,他们可以在此之后登录
- authenticate(**kwargs) 返回正确的用户
在 settings.py 中我有:
- The user is being registered correctly, they can log in after this
- authenticate(**kwargs) is returning the correct user
In settings.py I have:
AUTHENTICATION_BACKENDS = ('django.contrib.auth.backends.ModelBackend',)
谢谢!
def register(request):
user_creation_form = UserCreationForm(request.POST or None)
if request.method == 'POST' and user_creation_form.is_valid():
u_name = user_creation_form.cleaned_data.get('username')
u_pass = user_creation_form.cleaned_data.get('password2')
user_creation_form.save()
print u_name # Prints correct username
print u_pass # Prints correct password
user = authenticate(username=u_name,
password=u_pass)
print 'User: ', user # Prints correct user
login(request, user) # Seems to do nothing
return HttpResponseRedirect('/book/') # User is not logged in on this page
c = RequestContext(request, {'form': user_creation_form})
return render_to_response('register.html', c)
推荐答案
啊!我想到了.如果有人遇到这个问题,如果您手动调用它,请从 django.contrib.auth 导入登录 - 我正在导入视图.注释掉的代码表示对我的情况不利.
Ah! I figured it out. In case anyone has this issue, import login from django.contrib.auth if you are calling it manually - I was importing the view. Commented out code represents the bad import for my situation.
# from django.contrib.auth.views import login
from django.contrib.auth import authenticate, logout, login
这篇关于用户注册后Django自动登录(1.4)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文