扩展UserCreationForm以包括电子邮件,名字和姓氏 [英] Extending UserCreationForm to include email, first name, and last name

查看:520
本文介绍了扩展UserCreationForm以包括电子邮件,名字和姓氏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经停留了一段时间,似乎无法弄清楚发生了什么。我刚刚开始学习Django,我的登录设置,现在想实现一个注册页面。



我首先使用了UserCreationForm表单,但是工作正常,但是我想添加电子邮件,名字和姓氏的字段。我想我可以只是子类化UserCreationForm并添加字段,但似乎不起作用。另外我试图覆盖保存方法,但仍然不起作用。



我的自定义表单如下所示:



$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $
$ email = forms.EmailField(max_length = 75)

class Meta:
model =用户
fields =(first_name,last_name,email)

def save(self,commit = True):
user = super(RegistrationForm,self).save(commit = False)
user.first_name = self.cleaned_data [first_name ]
user.last_name = self.cleaned_data [last_name]
user.email = self.cleaned_data [email]
如果提交:
user.save()
返回用户

要处理的视图如下:

  def注册(请求):
如果request.method =='POST':
form = RegistrationForm(request.POST)
如果form.is_valid():
new_user = form.save();
new_user = authenticate(username = request.POST ['username'],password = request.POST ['password1'])
login(request,new_user)
return HttpResponseRedirect('/ members / home')
else:
form = RegistrationForm()

返回render_to_response('register.html',{'form':form},context_instance = RequestContext(request) )

表单加载新字段和所有内容,但是当我提交我收到错误:$ /

$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ b

哦,我也在使用Django 1.3。



任何想法我在做错什么?

解决方案

我的猜测是你的元类字段不包括 username



您正在从 UserCreationForm 继承表单字段,但不保存到用户 model,因此认证失败,并且崩溃在$ code> login()



文档建议您使用 login() authenticate()是否成功c>


I've been stuck on this for a while now and can't seem to figure out what's going on. I'm just starting to learn Django and I got my login set up and now want to implement a registration page.

I used the UserCreationForm form at first and that worked fine, but I want to add fields for Email, First name, and Last name. I figured I could just subclass UserCreationForm and add the fields but that doesn't seem to work. Also I tried overriding the save method, but it still doesn't work.

My custom form looks like this:

class RegistrationForm(UserCreationForm):
    first_name = forms.CharField(max_length=30)
    last_name = forms.CharField(max_length=30)
    email = forms.EmailField(max_length=75)

    class Meta:
        model = User
        fields = ("first_name", "last_name", "email",)

    def save(self, commit=True):
        user = super(RegistrationForm, self).save(commit=False)
        user.first_name = self.cleaned_data["first_name"]
        user.last_name = self.cleaned_data["last_name"]
        user.email = self.cleaned_data["email"]
        if commit:
            user.save()
        return user

The view to handle this is the following:

def Register(request):
    if request.method == 'POST':
        form = RegistrationForm(request.POST)
        if form.is_valid():
            new_user = form.save();
            new_user = authenticate(username=request.POST['username'], password=request.POST['password1'])
            login(request, new_user)
            return HttpResponseRedirect('/members/home')
    else:
        form = RegistrationForm()

    return render_to_response('register.html', {'form' : form}, context_instance=RequestContext(request))

The form loads just fine with the new fields and everything, but when I submit I get the error:

AttributeError at /register/

'AnonymousUser' object has no attribute 'backend'

Oh, and also I'm using Django 1.3.

Any idea what I'm doing wrong?

解决方案

My guess is that your meta class fields doesn't include username.

You are inheriting the form field from UserCreationForm but it's not saving to the User model and therefore authenticate is failing, and crashing on login()

The docs suggest you check whether authenticate() is successful before using login()

这篇关于扩展UserCreationForm以包括电子邮件,名字和姓氏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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