django allauth自定义注册表单以分配不同的组 [英] django allauth custom signup form to assign different groups
本文介绍了django allauth自定义注册表单以分配不同的组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在系统中有两种类型的用户,我想在注册时分配适当的组。请参考如何在使用django-allauth时自定义用户配置文件,我以为我可以覆盖Signup表单并执行类似的操作:
I have two types of users in the system, I want to assign the appropriate group at the time of signing up. Referring to How to customize user profile when using django-allauth, I thought I can override the Signup form and do something like:
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()
但是我仍然收到以下错误:
But I keep getting the error below:
save() missing 1 required positional argument: 'user'
此外,我已将allauth配置为使用电子邮件登录。
Also, I have configured allauth to use email to login.
感谢您的帮助。
推荐答案
注册是覆盖未保存的方法。
signup is the method to override not save.
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'
这篇关于django allauth自定义注册表单以分配不同的组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文