如何在django中更改密码而不使用认证系统?

2024-04-19 17:57:00 发布

您现在位置:Python中文网/ 问答频道 /正文

我想在不使用身份验证系统的情况下更改密码。密码正在更改,但我没有收到成功消息,而是收到错误消息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'),

Tags: name消息密码returnrequesttypepasswordchange
1条回答
网友
1楼 · 发布于 2024-04-19 17:57:00

这是因为在try块中没有返回任何内容。一个message不是你想象的HttpResponse

您应该显式返回一个HttpResponse,表示密码已成功更改

请注意,视图中的每个分支都必须返回可用的内容。因此except块同样需要固定

相关问题 更多 >