简体   繁体   English

Django - 使用 javascript 弹出确认框停止注销

[英]Django - stop logout with javascript pop-up confirm box

In my django site I have a logout button that redirects to the view logout .在我的 django 站点中,我有一个注销按钮,可以重定向到视图logout When the button is clicked it instantly logs the user out, but I would like a JS pop-up confirm box to appear then the logout button is clicked.单击该按钮时,它会立即注销用户,但我希望出现一个 JS 弹出确认框,然后单击注销按钮。

When the user clicks 'Ok' OR 'Cancel' it logs the user out.当用户单击“确定”或“取消”时,它会将用户注销。 How can i prevent the logout view being called when the user clicks 'Cancel'?当用户单击“取消”时,如何防止调用logout视图?

views.py视图.py

def logout(request):
    if "user_info" in request.session:
        del request.session["user_info"]
    #redirect to login so the user can log back in
    return redirect("login")

script.js脚本.js

function logout_popup() {
    if (confirm("Are you sure?")) {
        window.location.reload()
    }
}

base.html基地.html

<li onclick="logout_popup()" id="logout-tab"><a href="{% url 'logout' %}">Logout</a></li>

Try to move the onclick to the a tag:尝试将onclick移动到a标签:

<li id="logout-tab"><a onclick="logout_popup(event)" href="{% url 'logout' %}">Logout</a></li>

and the script:和脚本:

function logout_popup(e) {
    if (confirm("Are you sure?")) {
        window.location.reload()
    } else {
        e.preventDefault()
    }
}

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

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