I need to prevent showing previous pages from my django app

77 Views Asked by At

Even after I logged out I'm still able to see my previous pages how to prevent the browser from showing this? I tried this piece of JavaScript code . Note: I used template inheriting and templates statically imported.

function preventBack() {
  history.pushState(null, null, location.href);
  window.onpopstate = function () {
      history.go(1);
      console.log("worked")
  };
}

window.onload = function() {
  preventBack();
  console.log("worked call ")
};

when I tried this "console showed worked call" but the 'worked' is not yet printed in console means wins=down.onpopState not worked what's the cause of this problem. I'm quiet new in Django feels like I tarped here!!

1

There are 1 best solutions below

0
Abhishek On

It's possible that the onpopstate event handler is not being triggered because you are using the Django redirect function to log out the user. When you use redirect, the browser receives a new URL and navigates to that URL, which creates a new entry in the browser's history. This means that when the user clicks the back button, they will go back to the previous page, which was the page that was displayed before the redirect.

To prevent this, you can modify your preventBack function to also remove the current history entry after pushing a new one. This will ensure that there is only one entry in the history, which is the current page. Here's the modified function:

function preventBack() {
  history.pushState(null, null, location.href);
  window.onpopstate = function () {
      history.go(1);
  };
  window.history.pushState(null, "", window.location.href);
}