Reputation: 46284
Is it possible to detect a browser refresh by a user within a Django request?
Upvotes: 4
Views: 3242
Reputation: 92567
By itself, I believe there would not be a way to know a page was refreshed just by looking at a request object. A client could easily have opened a second version of the page. You would also have to preserve some state data about all of the request in order to compare something like timestamps, but that would all just be assumptions about what really happened...
What you can probably do is a combination of checking the request in django, and some javascript magic. The reason for this, is the client side is the only one that really knows if a page refresh is happening and must record some state for this action. A quick search turned up this little tutorial here: Detecting Page Refreshes :: Using JavaScript on Client-Side. It catches the page unload and sets cookie values, which is then checks on the next reload. You may be able to use part of that approach to set the cookie, and then check the request.cookies in your django view to see if that request recorded a page reload event.
*Note: That javascript page I linked is a bit old, but the information is still relevant. Here is a similar SO question
Upvotes: 1