代码之家  ›  专栏  ›  技术社区  ›  Roberto Fernandez Diaz

自定义社交点唱表单

  •  1
  • Roberto Fernandez Diaz  · 技术社区  · 7 年前

    我有一个自定义表单已经在正常登录(通过电子邮件),现在我有一个社会化的问题。

    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
    

    我的代码是:

    设置.py

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

    表格.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.implePerlyconfigured:导入表单类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 回复  |  直到 7 年前
        1
  •  1
  •   Roberto Fernandez Diaz    7 年前

    最后我发现了问题,

    默认情况下,它拥有的所有身份验证

    SOCIALACCOUNT_AUTO_SIGNUP = True
    

    我不知道为什么,它解释说属性本地化是由社会账户提供的,而不是。

    因此,解决方案是将其设置为false,并使用常规方法自定义表单( more info here )

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