扩展 UserCreationForm 以包含电子邮件、名字和姓氏

发布于 2024-12-12 06:35:38 字数 1595 浏览 0 评论 0原文

我已经被困在这个问题上有一段时间了,似乎无法弄清楚发生了什么。我刚刚开始学习 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

鹿港巷口少年归 2024-12-19 06:35:38

我的猜测是您的元类fields不包含username

您从 UserCreationForm 继承表单字段,但它没有保存到 User 模型,因此 authenticate 失败,并在 登录时崩溃()

文档建议您在使用 login() 之前检查 authenticate() 是否成功

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()

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文