扩展 UserCreationForm 以包含电子邮件、名字和姓氏
我已经被困在这个问题上有一段时间了,似乎无法弄清楚发生了什么。我刚刚开始学习 Django,我设置了登录,现在想要实现一个注册页面。
我首先使用 UserCreationForm 表单,效果很好,但我想添加电子邮件、名字和姓氏字段。我想我可以继承 UserCreationForm 并添加字段,但这似乎不起作用。我也尝试重写保存方法,但它仍然不起作用。
我的自定义表单如下所示:
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
处理此问题的视图如下:
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))
表单可以很好地加载新字段和所有内容,但是当我提交时,我收到错误:
AttributeError at /register/
'AnonymousUser' object has no attribute 'backend'
哦,而且我正在使用 Django 1.3。
知道我做错了什么吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我的猜测是您的元类
fields
不包含username
。您从
UserCreationForm
继承表单字段,但它没有保存到User
模型,因此authenticate
失败,并在登录时崩溃()
文档建议您在使用
login()
之前检查authenticate()
是否成功My guess is that your meta class
fields
doesn't includeusername
.You are inheriting the form field from
UserCreationForm
but it's not saving to theUser
model and thereforeauthenticate
is failing, and crashing onlogin()
The docs suggest you check whether
authenticate()
is successful before usinglogin()