Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 用户在更改密码后将注销_Python_Django_Django Views - Fatal编程技术网

Python 用户在更改密码后将注销

Python 用户在更改密码后将注销,python,django,django-views,Python,Django,Django Views,我有一个允许用户更改密码的视图。我不想使用默认密码身份验证系统。当用户更改其密码时,密码已成功更改,但用户正在注销。我看到会话在更改密码时变得无效。我不想在更改密码后再次登录。因此,如何在更改密码后保留会话 my views.py文件 from django.contrib.auth import update_session_auth_hash def password_change(request): if request.method == 'POST': new_

我有一个允许用户更改密码的视图。我不想使用默认密码身份验证系统。当用户更改其密码时,密码已成功更改,但用户正在注销。我看到会话在更改密码时变得无效。我不想在更改密码后再次登录。因此,如何在更改密码后保留会话

my views.py文件

from django.contrib.auth import update_session_auth_hash
def password_change(request):
    if request.method == 'POST':
        new_password = request.POST.get('pwd') 
        user=request.user  
        try:  
            u = User.objects.get(username=user)
            u.set_password(new_password)
            u.save()
            update_session_auth_hash(request,user)
            messages.add_message(request, messages.INFO, 'Password successfully changed')   
            return redirect("password_change") 
        except User.DoesNotExist:
            messages.add_message(request, messages.INFO, 'User Does not exist')   
    else:
        return render(request,"password_change.html",{})

您应该在update\u session\u auth\u hash中使用变量u。您正在使用请求中的“用户”,该用户仍然具有旧密码