用户注册后Django的自动登录(1.4) [英] Django automatic login after user registration (1.4)
本文介绍了用户注册后Django的自动登录(1.4)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个问题,我注册成功的用户 - 不过,我希望用户在注册登录。这里是code,它重新presents我的注册视图。在为什么用户是不是有什么想法自动登录?
注:
- 用户正在正确注册,他们可以登录本 之后
- 验证(** kwargs)将返回正确的用户
-
在settings.py我有:
AUTHENTICATION_BACKENDS =('django.contrib.auth.backends.ModelBackend',)
谢谢!
DEF注册(要求):
user_creation_form = UserCreationForm(request.POST或无)
如果request.method =='POST'和user_creation_form.is_valid():
u_name = user_creation_form.cleaned_data.get(用户名)
u_pass = user_creation_form.cleaned_data.get('密码2)
user_creation_form.save()
打印u_name#打印正确的用户名
打印u_pass#打印正确的密码
用户身份验证=(用户名= u_name,
密码= u_pass)
打印用户:用户#打印正确的用户
登录(请求用户)#似乎什么都不做
返回的Htt presponseRedirect('/电子书/')#用户没有登录在本页面
C =的RequestContext(请求,{形式:user_creation_form})
返回render_to_response('register.html',C)
解决方案
啊!我想到了。如果任何人有从django.contrib.auth如果要手动调用它这个问题,导入登录 - 我被导入视图。注释掉code再presents坏进口我的情况。
#从django.contrib.auth.views导入登录
从django.contrib.auth进口身份验证,注销,登录
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?
Notes:
- 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',)
Thanks!
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)
解决方案
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屋!
查看全文