1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
| class UserRegisterCreationForm(UserCreationForm): nickname = forms.CharField( max_length=100, required=False, label='昵称', help_text='请输入您的昵称(可选)', widget=forms.TextInput(attrs={ 'class': 'form-control', 'placeholder': '请输入您的昵称' }) ) signature = forms.CharField( widget=forms.Textarea(attrs={ 'rows': 3, 'class': 'form-control', 'placeholder': '请输入您的个人签名' }), required=False, label='签名', help_text='请输入您的个人签名(可选)' ) class Meta: model = User fields = ('username', 'password1', 'password2') labels = { 'username': '用户名', 'password1': '密码', 'password2': '确认密码', } widgets = { 'username': forms.TextInput(attrs={ 'class': 'form-control', 'placeholder': '请输入用户名' }), 'password1': forms.PasswordInput(attrs={ 'class': 'form-control', 'placeholder': '请输入密码' }), 'password2': forms.PasswordInput(attrs={ 'class': 'form-control', 'placeholder': '请再次输入密码' }), } def save(self, commit=True): user = super().save(commit=False) if commit: user.save() user_profile, created = UserProfile.objects.get_or_create( user=user, defaults={ 'nickname': self.cleaned_data['nickname'], 'signature': self.cleaned_data['signature'] } ) if not created: user_profile.nickname = self.cleaned_data['nickname'] user_profile.signature = self.cleaned_data['signature'] user_profile.save() return user_profile
|