自定义社交唱歌形式

时间:2018-05-22 00:19:45

标签: django django-allauth

我有一个自定义表单已经正常登录(使用电子邮件),现在我遇到社交问题。

Following the official documentation

from allauth.socialaccount.forms import SignupForm
class MyCustomSocialSignupForm(SignupForm):

    def save(self):

        # Ensure you call the parent classes save.
        # .save() returns a User object.
        user = super(MyCustomSocialSignupForm, self).save()

        # Add your own processing here.

        # You must return the original result.
        return user

我的代码如下:

settings.py

ACCOUNT_SIGNUP_FORM_CLASS = 'accounts.forms.SignupFormEmail'
SOCIALACCOUNT_FORMS = {'signup': 'accounts.forms.SignupFormSocial'}

forms.py

class SignupFormEmail(forms.Form):

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)

        self.fields['localizacion'].label = 'Categorias'

    localidades = Alcance.objects.exclude(pk=1).filter(zona_afectada=Alcance.CONCEJO)

    localizacion = forms.ModelChoiceField(queryset=localidades, label='Concejo',empty_label="Selecciona tu concejo")

    def signup(self, request, user: User):
        self.user=user
        user.residencia.add(self.cleaned_data['localizacion'])
        user.save()



class SignupFormSocial(SignupForm):

    localidades = Alcance.objects.exclude(pk=1).filter(zona_afectada=Alcance.CONCEJO)

    localizacion = forms.ModelChoiceField(queryset=localidades, label='Concejo', empty_label="Selecciona tu concejo")

    def __init__(self, sociallogin=None, *args, **kwargs):
        super().__init__(*args, **kwargs)

        self.fields['localizacion'].label = 'Categorias'


    def save(self,request):
        # Ensure you call the parent classes save.
        # .save() returns a User object.
        user = super(SignupFormSocial, self).save() 
        # Add your own processing here.
        user.residencia.add(self.cleaned_data['localizacion'])
        user.save()

        # You must return the original result.
        return user

问题是我得到以下异常django.core.exceptions.ImproperlyConfigured:导入表单类accounts.forms时出错:“无法导入名称'BaseSignupForm'”

我也尝试,继承自form.Form和SignupFormEmail,但它们都不起作用

我该怎么做?

在设置中更新了已安装的应用:

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'django.contrib.sites',
    # Django AllAuth
    'allauth',
    'allauth.account',
    'allauth.socialaccount',
    'allauth.socialaccount.providers.facebook',
    'allauth.socialaccount.providers.google',
    'allauth.socialaccount.providers.twitter',
    #My apps
    'accounts.apps.AccountsConfig',

1 个答案:

答案 0 :(得分:0)

最后我发现了问题,

所有身份验证默认情况下

SOCIALACCOUNT_AUTO_SIGNUP = True

我不知道为什么,它解释了属性本地化是由社交帐户给出的,而不是。

所以解决方案是将其设置为false并使用常规方式自定义表单(more info here

ACCOUNT_SIGNUP_FORM_CLASS = 'accounts.forms.SignupForm'
SOCIALACCOUNT_AUTO_SIGNUP = False