我想在不使用身份验证系统的情况下更改密码。密码正在更改,但我没有收到成功消息,而是收到错误消息ValueError: The view books.views.password_change didn't return an HttpResponse object. It returned None instead
视图.py
def password_change(request):
if request.method == 'POST':
new_password = request.POST.get('pwd2')
user=request.user
try:
u = User.objects.get(username=user)
u.set_password(new_password)
u.save() #problem line
messages.add_message(request, messages.INFO, 'Password Changed')
except User.DoesNotExist:
messages.add_message(request, messages.INFO, 'User Does not exist')
else:
return render(request,"password_change.html",{})
我的模板文件
<form method="POST">
{% csrf_token %}
<div>
<p>Username:{{request.user}}</p>
<input type="password" name="pwd1" placeholder="Password">
<input type="password" name="pwd2" placeholder="Confirm Password">
<button type="submit">Update Password</button>
</div>
</form>
url文件
url(r'^password_change/$',views.password_change,name='password_change'),
这是因为在
try
块中没有返回任何内容。一个message
不是你想象的HttpResponse
您应该显式返回一个
HttpResponse
,表示密码已成功更改请注意,视图中的每个分支都必须返回可用的内容。因此
except
块同样需要固定相关问题 更多 >
编程相关推荐