I'm using this code (in forms.py) for my custom signup form for allauth:
class RegistrationForm(UserCreationForm):
birth_date = forms.DateField(widget=extras.SelectDateWidget(years=BIRTH_DATE_YEARS))
class Meta:
model = get_user_model()
fields = ('username', 'email', 'password1', 'password2', 'first_name', 'last_name', 'gender', 'birth_date', 'city', 'country')
I have of course specified ACCOUNT_SIGNUP_FORM_CLASS
in settings.py to point to this form and it displays the fields which I've put in it. However, it does not work on submission, even if I put signup
method it never gets invoked. I've tried with the save
but still it's the same - it raises an error that one of my custom added fields is blank while creating user and saving it to the database.
So, what's the correct code for achieving this?
One of the most common queries about allauth is about adding additional fields or custom fields to the signup form. You can extend the SignupForm class from allauth.account. forms. All you need to do is create a custom class pass the SignupForm to the custom class and define the custom fields and save it.
django-allauth is an integrated set of Django applications dealing with account authentication, registration, management, and third-party (social) account authentication. It is one of the most popular authentication modules due to its ability to handle both local and social logins.
I've made it thanks to @rakwen who has found the correct solution here. I wrote my custom adapter and put it in adapters.py in my app:
from allauth.account.adapter import DefaultAccountAdapter
class AccountAdapter(DefaultAccountAdapter):
def save_user(self, request, user, form, commit=False):
data = form.cleaned_data
user.username = data['username']
user.email = data['email']
user.first_name = data['first_name']
user.last_name = data['last_name']
user.gender = data['gender']
user.birth_date = data['birth_date']
user.city = data['city']
user.country = data['country']
if 'password1' in data:
user.set_password(data['password1'])
else:
user.set_unusable_password()
self.populate_username(request, user)
if commit:
user.save()
return user
Then I've specified ACCOUNT_ADAPTER
in settings.py to point to that adapter, and it finally started to work!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With