[英]str object has no attribute '*'
I wanna update the password of my user but i have the error 我想更新用户密码,但出现错误
str object has no attribute '*'
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()
The problem is on the line u.set_password(passwordNew)
. 问题在
u.set_password(passwordNew)
。
The u
is not instance of User
model as you intended but a string value coming from POST form. u
不是您想要的User
模型实例,而是来自POST表单的字符串值。 What you have to do is to get User
instance due to what username you got in your form field 由于您在表单字段中获得的
User
名,您需要做的是获取User
实例
u = User.objects.get(username=request.POST.get('username', ''))
You also have to handle the situation when there is no such user with given username 当没有给定用户名的用户时,您也必须处理这种情况
try:
u = User.objects.get(username=request.POST.get('username', ''))
#setting password and whatever...
except User.DoesNotExist:
#do something
You can get it as follow: 您可以按以下方式获取它:
u = request.POST.get('username', '')
NewPassword=set_password(passwordNew)
userobj = User.objects.get(username=u)
query = User.objects.filter(username=userobj).update(password=NewPassword)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.