0
我有FormView从表单类UserCreateForm的注册表单。它workf罚款,但现在我需要登录的用户注册后自动,我有下面的代码:如何在使用Django的FormView中注册后自动登录用户?
class RegisterView(FormView):
form_class = UserCreateForm
template_name = 'contadores/register.html'
success_url = '/contadores/create/'
def dispatch(self, request, *args, **kwargs):
if request.user.is_authenticated():
return HttpResponseRedirect('/contadores/create')
else:
return super(RegisterView, self).dispatch(request, *args, **kwargs)
def form_valid(self,form):
user = form.save()
user = authenticate(username=request.POST['username'],password=request.POST['password'])
login(self.request,user)
return super(RegisterView,self).form_valid(form)
我与clean_data试图代替request.POST但无论如何给出一个错误,我也试图与登录(自我。请求,form.user_cache)在form_valid函数,但我不能得到它。问题是:我如何得到这个?
它不工作,它说:“名字‘请求’没有定义 ” – dfrojas
哦,那是因为你要通过'request'到'form_valid()'。更新了答案。 – xyres
谢谢,但我想我必须将请求传递给Super(RegisterView,self).form_valid(form),因为我用新的django(和python)来传递请求吗?因为django说缺少一个参数 – dfrojas