Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/logging/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Django表单以更改密码_Django_Django Forms_Django Views - Fatal编程技术网

Django表单以更改密码

Django表单以更改密码,django,django-forms,django-views,Django,Django Forms,Django Views,当用户成功登录并进入主页时,会出现一个用于更改密码的链接“更改密码”。该链接显示一个更改密码的表单,其中有三个输入框用于输入旧密码、新密码和确认新密码 这是我的密码 forms.py class reset_form(forms.Form): oldpassword = forms.CharField(max_length = 20, widget=forms.TextInput(attrs={'type':'password', 'placeholder':'your old Pa

当用户成功登录并进入主页时,会出现一个用于更改密码的链接“更改密码”。该链接显示一个更改密码的表单,其中有三个输入框用于输入旧密码、新密码和确认新密码

这是我的密码

forms.py

class reset_form(forms.Form):


    oldpassword = forms.CharField(max_length = 20, widget=forms.TextInput(attrs={'type':'password', 'placeholder':'your old Password',  'class' : 'span'}))
    newpassword1 = forms.CharField(max_length = 20, widget=forms.TextInput(attrs={'type':'password', 'placeholder':'New Password',  'class' : 'span'}))
    newpassword2 = forms.CharField(max_length = 20, widget=forms.TextInput(attrs={'type':'password', 'placeholder':'Confirm New Password',  'class' : 'span'}))


    def clean(self):
        if 'newpassword1' in self.cleaned_data and 'newpassword2' in self.cleaned_data:
            if self.cleaned_data['newpassword1'] != self.cleaned_data['newpassword2']:
                raise forms.ValidationError(_("The two password fields did not match."))
        return self.cleaned_data
视图.py

class reset_form(forms.Form):


    oldpassword = forms.CharField(max_length = 20, widget=forms.TextInput(attrs={'type':'password', 'placeholder':'your old Password',  'class' : 'span'}))
    newpassword1 = forms.CharField(max_length = 20, widget=forms.TextInput(attrs={'type':'password', 'placeholder':'New Password',  'class' : 'span'}))
    newpassword2 = forms.CharField(max_length = 20, widget=forms.TextInput(attrs={'type':'password', 'placeholder':'Confirm New Password',  'class' : 'span'}))


    def clean(self):
        if 'newpassword1' in self.cleaned_data and 'newpassword2' in self.cleaned_data:
            if self.cleaned_data['newpassword1'] != self.cleaned_data['newpassword2']:
                raise forms.ValidationError(_("The two password fields did not match."))
        return self.cleaned_data


在使用正确的旧密码提交表单后,我收到了此消息您输入了错误的旧密码我不知道为什么我会发送此错误消息请帮助处理此代码出于某种原因,您正在通过
请求使用数据库中存储的密码字段。用户
,而不是他们在表格中实际输入的一个。数据库版本是散列的,当您调用
authenticate
时,它会再次对其进行散列,因此无法匹配

您应该使用用户在表单中输入的值:

username = request.user.username
password = form.cleaned_data['oldpassword']

user = authenticate(username=username, password=password)

谢谢丹尼尔的回答,它真的帮助了我。但现在我又遇到了另一个问题,我的密码被更改了,但改成了其他文本。现在,我的密码既不是旧密码,也不是我提供的新密码。我该怎么办?你怎么决定?别忘了,正如我在回答中提到的,密码是散列存储在数据库中的。