[Django]-Force reload on client's webpage Django

4👍

There’s a couple of options:

  • Add a script on your page that polls (ajax) your server every few seconds for the latest data and refresh the data in place (no page refresh involved). For this you’d create a view in Django that returns the data in JSON format and your javascript would replace the data in the HTML. Pro: great user experience, page remains available. Con: Not immediate, you’d have to think of scalability as each open page will be constantly polling your server, but caching the response should be easy.

  • Add a script that just polls whether the page should be refreshed. It would send some kind of version number of token identifying the current data on the page and your Django view could tell whether or not the page needs to be refreshed. Then the script would just call location.reload() to refresh the page. Pro: no need to fetch the data, refresh state can easily be cached, Con: still not immediate, page refresh happens when user might not be expecting it.

  • Use a websocket to have an open permanent connection open to your server through which you can push the new data (or just the event: ‘please refresh’). Pro: best experience, immediate update, Con: doesn’t work on old browsers, complex to implement and deploy. django-channels would be a good starter.

0👍

location.reload() 

Inside the HTML inside of a Django template should suffice. Do you need this to fire on a specific event? If so, you’ll want to find a way to trigger location.reload(), which in that case you’ll need to provide a few more specifics. But essentially you’d just have some JavaScript listening for the event to reload your webpage. If you want to get advanced I’d look into Django Channels.

-4👍

Try this in your .html code. Reloads page every minute

{% block content %}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
    setTimeout(function(){
       location.reload(true);
       alert("The page will now refresh");
    }, 60000);  
});
</script>
{% endblock content %}
👤Goran

Leave a comment