I am making a web app using Django/Python. There is a navigation at the top of the web app, and if a user clicks on the 'logout' link a pop-up window using Javascript's window.confirm() asks them if they are sure that they want to log out.
I have the following in logout.js
:
function logout() {
if(window.confirm("Are you you sure that you want to log out?")) {
window.open("index.html");
}
return;
}
If a user selects 'OK' in the pop-up that is displayed, I want the app to log the user out and display the home page of the app (represented by index.html
). However, to log the user out I need to do this in views.py. I have the following code in views.py:
def logoutUser(request):
if request.POST:
if request.POST.get("confirm-btn"):
logout(request)
return render(request, 'index.html')
if request.POST.get("cancel-btn"):
return redirect('/dashboard/')
return render(request, 'logout.html')
I have a page called logout.html which has two buttons, and if a user clicks the confirm button they are logged out and taken to the home page. However, I want a pop-up to be displayed instead.
My question is, how can I connect the Javascript code for the logout pop-up with the backend code in views.py
that deals with the user logging out. If a user presses 'OK' in the window.confirm()
, how can I actually log the user out using logout(request)
and display index.html
of the app? Any insights are appreciated.