简体   繁体   English

request.post 变量总是返回为空

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

So I'm trying to see if user matches their old password, if they don't then it will give a message saying "Your old password in incorrect" but if they match their old password then it should check if user matched new and confirmed password correctly, if they did not then it should give a message saying "Your new passwords do not match" but if their password is empty it should say "Your new passwords are empty, But even if my passwords are not empty it always says my passwords are empty. I have also tried if new_password1 or new_password2 == '' but that also has the same result.所以我试图查看用户是否匹配他们的旧密码,如果他们不匹配,那么它会给出一条消息说“你的旧密码不正确”但是如果他们匹配他们的旧密码,那么它应该检查用户是否匹配新的并确认密码正确,如果他们没有,那么它应该给出一条消息说“你的新密码不匹配”,但如果他们的密码是空的,它应该说“你的新密码是空的,但是即使我的密码不是空的,它总是说我密码是空的。我也尝试过if new_password1 or new_password2 == ''但这也有相同的结果。

views.py视图.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')

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>

you need to change你需要改变

if new_password1 or new_password2 is None:

to

if new_password1 is None or new_password2 is None:

if new_password1 is any truthy value... it will evaluate new_password1 or new_password2 is None : as True or new_password2 is None ... which is clearly True... (You also probably == '' instead of 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