It appears that there are multiple ways to add a simple field to a django-allauth
signup form.
From @danielfeldroy I see what's quoted below.
# 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()
But then, in the response to a ticket from https://github.com/pennersr/django-allauth/issues/826 pennersr (django-allauth's founder) states that all we have to do is:
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()
And then add ACCOUNT_SIGNUP_FORM_CLASS = 'yourproject.yourapp.forms.SignupForm'
to my settings.
See: How to customize user profile when using django-allauth
But then in the docs we have this: https://django-allauth.readthedocs.io/en/latest/forms.html#signup-allauth-account-forms-signupform
Where we do this:
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
And we add ACCOUNT_FORMS = {'signup': 'mysite.forms.MyCustomSignupForm'}ACCOUNT_FORMS = {'signup': 'mysite.forms.MyCustomSignupForm'}
to the settings file.
So my basic question, if we assume we just want to add a couple of fields, which is the right one?
Because I have a really basic setup and none of the approaches work.
I get close with the one where we inherit the forms.Form
. Well, I don't get any errors. But it doesn't actually save the input. Even though I can see the cleaned()
input data through with print()
statements.
I'm really confused and I hope that someone can help me sort out what is best approach.
Here's what I have.
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
And then in settings I have ACCOUNT_SIGNUP_FORM_CLASS = 'users.forms.CustomSignupForm'