django-allauth - Overriding default signup form

前端 未结 2 1079
孤街浪徒
孤街浪徒 2021-02-04 17:38

I\'m using this code (in forms.py) for my custom signup form for allauth:

class RegistrationForm(UserCreationForm):
    birth_date = forms.DateField(widget=extra         


        
2条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-02-04 18:03

    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!

提交回复
热议问题