繁体   English   中英

request.post 变量总是返回为空

[英]request.post variable is always returning as empty

所以我试图查看用户是否匹配他们的旧密码,如果他们不匹配,那么它会给出一条消息说“你的旧密码不正确”但是如果他们匹配他们的旧密码,那么它应该检查用户是否匹配新的并确认密码正确,如果他们没有,那么它应该给出一条消息说“你的新密码不匹配”,但如果他们的密码是空的,它应该说“你的新密码是空的,但是即使我的密码不是空的,它总是说我密码是空的。我也尝试过if new_password1 or new_password2 == ''但这也有相同的结果。

视图.py

@login_required
def userchange(request):
    if request.method == 'POST':
        user = request.user
        old_password = request.POST['Old password']
        new_password1 = request.POST['New password1']
        new_password2 = request.POST['New password2']
        if user.check_password(old_password):
            if new_password1 == new_password2:
                if new_password1 or new_password2 is None:
                    return render(request, 'main/change.html', {'error':'Your new passwords are empty!'})
                else:
                    user.set_password(new_password1)
                return render(request, 'main/change.html', {'error':'Your password has been changed succesfully!'})
            else:
                return render(request, 'main/change.html', {'error':'Your new passwords do not match'})
        else:
            return render(request, 'main/change.html', {'error':'Your old password is incorrect'})
    elif request.method == "GET":
        return render(request, 'main/change.html')

更改.html

<h1>Note: You will be logged out</h1>
<h2>{{ error }}</h2>
<form method="POST">
    {% csrf_token %}
    <input type="password" placeholder="Old password" name="Old password">
    <input type="password" placeholder="New password" name="New password1">
    <input type="password" placeholder="Confirm password" name="New password2">
    <button type="submit">Change password</button>
</form>

你需要改变

if new_password1 or new_password2 is None:

if new_password1 is None or new_password2 is None:

如果new_password1是任何真实值...它将评估new_password1 or new_password2 is None :因为True or new_password2 is None ...这显然是 True...(您也可能== ''而不是is None

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM