1

Django question

I would like to ask whether it is possible or not to distinguish in view or in template if page was reloaded or page was reached by url from the other page? If shorten it out, need to know if it is any marker that would indicate that page got reloaded by F5 or whatever?

Perhaps it is probably possible to figure out by analyzing request.

Only 1 idea that comes to my mind is following:

request.get_full_path()# – specifies full address of the current page

request.META.get('HTTP_REFERER')# – specifies full address of previous page

So that theoretically if former == last – page is reloaded, but it doesn't work this way.

If you know any solution or hint -please communicate it.

Thank you and have a nice day!

Aleksei Khatkevich
  • 1,923
  • 2
  • 10
  • 27
  • 1
    Possible duplicate of [Is it possible to detect a browser refresh from a Django request?](https://stackoverflow.com/questions/11512608/is-it-possible-to-detect-a-browser-refresh-from-a-django-request) – Brown Bear May 03 '19 at 10:43
  • @Bear Brown. I read that one. There are only JS solution on the issue – Aleksei Khatkevich May 03 '19 at 10:45
  • this text i mean `By itself, I believe there would not be a way to know a page was refreshed just by looking at a request object.` – Brown Bear May 03 '19 at 10:46
  • [HTTP_REFERER and reload](https://stackoverflow.com/q/1262371) – djvg Jun 07 '23 at 14:26

1 Answers1

1

you can do one thing:

use one hidden element in your template like this :

 <input type="hidden" id="anyId" name="anyName" value="">

then call a fn to assign value to this element when page reloads

<body onbeforeunload="assignValue()>
<!--put your stuff-->
<script>
function assignValue(){
document.getElementById("anyId").value="reloaded";
}
</body>

because of onbeforeunload on page load assignValue() fn will be called. then you can check this value of input element in your backend if value is not empty i.e. "" means page is loaded

chirag soni
  • 936
  • 10
  • 20