我正在尝试使用用户的名字生成唯一的用户名,因为它未在注册表单中使用。
我不确定如何在forms.py模块中实现allauth.account.adapter.DefaultAccountAdapter。请帮助举个例子。
from allauth.account.forms import SignupForm, LoginForm
from allauth.account.adapter import get_adapter
class CustomSignupForm(SignupForm):
first_name = forms.CharField(max_length=30, label='First Name', widget=forms.TextInput(
attrs={'placeholder': 'First name', 'class': 'form-control'}))
last_name = forms.CharField(max_length=30, label='Last Name', widget=forms.TextInput(
attrs={'placeholder': 'Last name', 'class': 'form-control'}))
def __init__(self, *args, **kwargs):
super(CustomSignupForm, self).__init__(*args, **kwargs)
self.fields['email'].widget.attrs.update({'class': 'form-control'})
self.fields['password1'].widget.attrs.update({'class': 'form-control'})
self.fields['password2'].widget.attrs.update({'class': 'form-control'})
def signup(self, request, user):
adapter = get_adapter()
username = adapter.generate_unique_username(self, self.cleaned_data['first_name'])
user.username = username
user.first_name = self.cleaned_data['first_name']
user.last_name = self.cleaned_data['last_name']
user.save()