我想更新我的用户的密码,但我有错误
str对象没有属性'*'
if request.method == 'POST':
form = resetPwdForm(request.POST)
if form.is_valid():
email = form.cleaned_data['email']
passwordNew = form.cleaned_data['passwordNew']
passwordConfirm = form.cleaned_data['passwordConfirm']
#actual password is ok
if passwordConfirm == passwordNew:
#new password match confirm
u = request.POST.get('username', '')
u.set_password(passwordNew)
u.save()
Run Code Online (Sandbox Code Playgroud)
问题就在于此u.set_password(passwordNew).
该u不是实例User为你打算,但一个字符串值从POST表单来的模型.您需要做的是获取User实例,因为您在表单字段中获得了什么用户名
u = User.objects.get(username=request.POST.get('username', ''))
Run Code Online (Sandbox Code Playgroud)
当没有具有给定用户名的用户时,您还必须处理这种情况
try:
u = User.objects.get(username=request.POST.get('username', ''))
#setting password and whatever...
except User.DoesNotExist:
#do something
Run Code Online (Sandbox Code Playgroud)