我应该如何在 django-allauth 的注册表单中添加一个字段? Docs vs Stackoverflow vs 博客帖子

How am I supposed to add a field to a signup form in django-allauth? Docs vs Stackoverflow vs Blog posts

似乎有多种方法可以将简单字段添加到 django-allauth 注册表单。

来自@danielfeldroy 我看到下面引用的内容。

# SpyBookSignupForm inherits from django-allauth's SignupForm
class SpyBookSignupForm(SignupForm):

# Specify a choice field that matches the choice field on our user model
type = d_forms.ChoiceField(choices=[("SPY", "Spy"), ("DRIVER", "Driver")])

# Override the init method
def __init__(self, *args, **kwargs):
    # Call the init of the parent class
    super().__init__(*args, **kwargs)
    # Remove autofocus because it is in the wrong place
    del self.fields["username"].widget.attrs["autofocus"]

# Put in custom signup logic
def custom_signup(self, request, user):
    # Set the user's type from the form reponse
    user.type = self.cleaned_data["type"]
    # Save the user's type to their database record
    user.save()

但是,在对来自 https://github.com/pennersr/django-allauth/issues/826 pennersr(django-allauth 的创始人)的票的回复中指出,我们所要做的就是:

class SignupForm(forms.Form):
    first_name = forms.CharField(max_length=30, label='Voornaam')
    last_name = forms.CharField(max_length=30, label='Achternaam')

    def signup(self, request, user):
        user.first_name = self.cleaned_data['first_name']
        user.last_name = self.cleaned_data['last_name']
        user.save()

然后将 ACCOUNT_SIGNUP_FORM_CLASS = 'yourproject.yourapp.forms.SignupForm' 添加到我的设置中。

参见:How to customize user profile when using django-allauth

但是在文档中我们有这个:https://django-allauth.readthedocs.io/en/latest/forms.html#signup-allauth-account-forms-signupform

我们在哪里做:

from allauth.account.forms import SignupForm
class MyCustomSignupForm(SignupForm):

def save(self, request):

    # Ensure you call the parent class's save.
    # .save() returns a User object.
    user = super(MyCustomSignupForm, self).save(request)

    # Add your own processing here.

    # You must return the original result.
    return user

然后我们在设置文件中添加ACCOUNT_FORMS = {'signup': 'mysite.forms.MyCustomSignupForm'}ACCOUNT_FORMS = {'signup': 'mysite.forms.MyCustomSignupForm'}

所以我的基本问题是,如果我们假设我们只想添加几个字段,这是正确的?

因为我有一个非常基本的设置并且 none 的方法有效。

我接近了我们继承 forms.Form 的地方。好吧,我没有收到任何错误。但它实际上并没有保存输入。即使我可以通过 print() 语句看到 cleaned() 输入数据。

我真的很困惑,我希望有人能帮我找出最好的方法。

这是我的。

class CustomSignupForm(forms.Form):
    opt_in = forms.BooleanField(label="Add me to the email list", help_text="Don't worry. We won't spam you.", initial=True, required=False)

    def signup(self, request, user):
        user.opt_in = self.cleaned_data['opt_in']
        user.save

然后在设置中我有 ACCOUNT_SIGNUP_FORM_CLASS = 'users.forms.CustomSignupForm'

因此,如果这是您的逐字代码,错误是您没有调用保存方法,因为缺少括号:

class CustomSignupForm(forms.Form):
    opt_in = forms.BooleanField(label="Add me to the email list", help_text="Don't worry. We won't spam you.", initial=True, required=False)

    def signup(self, request, user):
        user.opt_in = self.cleaned_data['opt_in']
        user.save() # Note ()

这应该可以解决您的问题