django allauth自定义注册表单分配不同的组

时间:2014-11-28 00:35:33

标签: django django-allauth

我在系统中有两种类型的用户,我想在注册时分配相应的组。参考How to customize user profile when using django-allauth,我想我可以覆盖注册表单并执行以下操作:

class CustomSignupForm(forms.Form):
    login_widget = forms.TextInput(attrs={'type': 'email',
                                              'placeholder': _('email'),
                                              'autofocus': 'autofocus',
                                              'class': 'form-control'
                                      })

    email = forms.EmailField(label='Email', widget=login_widget)
    password = PasswordField(label='Password', widget=forms.PasswordInput(attrs={'class': 'form-control'}))
    password2 = PasswordField(label='Re-type Password', widget=forms.PasswordInput(attrs={'class': 'form-control'}))

    def save(self, request, user):
        role = request.GET.get('type')
        print(role)
        group = role or "group1"
        g = Group.objects.get(name=group)
        user.groups.add(g)
        user.save()

但我一直收到以下错误:

save() missing 1 required positional argument: 'user'

此外,我已将allauth配置为使用电子邮件登录。

感谢您的帮助。

1 个答案:

答案 0 :(得分:4)

注册是覆盖不保存的方法。

class LocalSignupForm(forms.Form):
    pass

    def signup(self, request, user):
        role = request.session.get('user_type')
        group = role or "Default"
        g = Group.objects.get(name=group)
        user.groups.add(g)
        user.save()

此外,设置必须

ACCOUNT_SIGNUP_FORM_CLASS = 'useraccount.forms.LocalSignupForm'