请帮助解决错误
我在forms.py
中检查用户的密码from django import forms
from userprofile.models import UserProfile
from django.contrib.auth.models import User
from django.forms import ModelForm
class ChangePasswordForm(forms.Form):
password_old = forms.CharField(max_length=30)
password1 = forms.CharField(widget=forms.PasswordInput())
password2 = forms.CharField(widget=forms.PasswordInput())
def clean_password_old(self):
cleaned_data = self.cleaned_data
password_old = cleaned_data.get("password_old")
if not User.check_password(password_old):
raise forms.ValidationError("Passwords is not correct")
else:
return cleaned_data
def clean(self):
cleaned_data = self.cleaned_data
password1 = cleaned_data.get("password1")
password2 = cleaned_data.get("password2")
if password1 != password2:
raise forms.ValidationError("Passwords must be same")
else:
return cleaned_data
但在屏幕上提交表单后会收到以下错误消息:
TypeError at /userprofile/password_page/
check_password() missing 1 required positional argument: 'raw_password'
Request Method: POST
Request URL: http://127.0.0.1:8000/userprofile/password_page/
Django Version: 1.6.2
Exception Type: TypeError
Exception Value:
check_password() missing 1 required positional argument: 'raw_password'
答案 0 :(得分:2)
这一行没有意义:
if not User.check_password(password_old):
您在User类上调用check_password
,而不是在实例上调用{{1}}。但您需要检查实际用户的密码,这意味着您必须实际拥有该用户。
无论如何,自己写这篇文章毫无意义。 Django已经包含一个带有表单的change_password视图:请参阅the documentation。