英文:
Django - stop logout with javascript pop-up confirm box
问题
以下是您要求的代码部分的中文翻译:
# views.py
def logout(request):
if "user_info" in request.session:
del request.session["user_info"]
# 重定向到登录页面,以便用户可以重新登录
return redirect("login")
// script.js
function logout_popup() {
if (confirm("您确定吗?")) {
window.location.reload();
}
}
<!-- base.html -->
<li onclick="logout_popup()" id="logout-tab"><a href="{% url 'logout' %}">注销</a></li>
英文:
In my django site I have a logout button that redirects to the view 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.
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'?
views.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
function logout_popup() {
if (confirm("Are you sure?")) {
window.location.reload()
}
}
base.html
<li onclick="logout_popup()" id="logout-tab"><a href="{% url 'logout' %}">Logout</a></li>
答案1
得分: 0
尝试将 onclick
移动到 a
标签:
<li id="logout-tab"><a href="{% url 'logout' %}" onclick="logout_popup(event)">注销</a></li>
以及脚本部分:
function logout_popup(e) {
if (confirm("确定要注销吗?")) {
window.location.reload();
} else {
e.preventDefault();
}
}
英文:
Try to move the onclick
to the a
tag:
<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()
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论