django-allauth - Overriding default signup form

我们两清 提交于 2019-12-03 03:33:47
MikiSoft

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!

Nathan Ingram

In your project setting.py try to add this line:

verify if you have added this:

AUTHENTICATION_BACKENDS = (
    "django.contrib.auth.backends.ModelBackend",
    "allauth.account.auth_backends.AuthenticationBackend",
)

ACCOUNT_SIGNUP_FORM_CLASS = "yourapp.forms.customSignupForm"

In app.models

class CustomModel(models.Model):
    """CustomModel docstring."""

    city = forms.CharField(max_length=75)
    country = forms.CharField(max_length=25)
    other....

In app.forms:

class SignForm(forms.ModelForm):
    """SignForm docstring."""

    username = forms.CharField(
        max_length=30,
    )
    first_name = forms.CharField(
        max_length=30,
    )
    last_name = forms.CharField(
        max_length=30,
    )
    field...

    def myclean():

    def signup(self, request, user):
        """You signup function."""


    # dont forget to save your model

    class Meta:
        model = mymodel.CustomModel
        fields = [
            'city',
            'country',
            'field...',
        ]

Try this method work !

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!