Tôi đang cố thiết lập mối quan hệ Người dùng - UserProfile, hiển thị biểu mẫu và lưu dữ liệu.Django, ModelForms, Người dùng và UserProfile - không băm mật khẩu
Khi được gửi, dữ liệu được lưu, ngoại trừ trường mật khẩu không bị băm.
Forms.py
class UserForm(forms.ModelForm):
username = forms.RegexField(label="Username", max_length=30,
regex=r'^[\[email protected]+-]+$', help_text = "My text",
error_messages = {'invalid':
"This value may contain only letters, numbers and @/./+/-/_ characters."
}
)
password = forms.CharField(label="Password",
widget=forms.PasswordInput)
class Meta:
model = User
fields = ["first_name", "last_name", "username", "email", "password"]
def clean_username(self):
username = self.cleaned_data['username']
if not re.search(r'^\w+$', username):
raise forms.ValidationError(
'Username can contain only alphanumeric characters')
try:
User.objects.get(username=username)
except ObjectDoesNotExist:
return username
raise forms.ValidationError('Username is already taken')
class UserProfileForm(forms.ModelForm):
class Meta:
model = UserProfile
fields = ['user_is']