我有一些代码
1.当我尝试更改用户密码时,存储在数据库中的密码为纯文本。
2.提交表单而不输入密码时,数据库密码为空。
我该怎么办?谢谢
class UserUpdate(LoginRequiredMixin, SuccessMessageMixin, UpdateView):
model = User
template_name = 'users/UserUpdate.html'
context_object_name = 'UserUpdate'
form_class = UserUpdateForm
success_url = reverse_lazy('UserList')
success_message = 'update success'
def form_valid(self, form):
password = form.cleaned_data.get('password')
if not password:
return super().form_valid(form)
return super().form_valid(form)
表格
class UserUpdateForm(ModelForm):
password = forms.CharField(
required=False, label="password",
max_length=32, strip=False,
widget=forms.PasswordInput,
)
class Meta:
model = User
fields = ['username', 'password', 'last_name', 'email', 'is_superuser', 'is_active']
def save(self, commit=True):
password = self.cleaned_data.get('password')
user = super().save(commit=commit)
if password:
user.reset_password(password)
else:
pass
return user