0

The task: i have a Chart that feeds of some data which is continuosly updated throughout the day. Specifically, the data is extracted from the web, it's written in the database and such database is read by a django view and later visualized in the aforementioned Chart.

The problem: I want the user to see the new data without reloading the page. Looking online, using Django, i think I have to link it to a rest api, the problem is that in practice i have no cliente how to set it up.

The question: is this the best way to do it? If so, could you point me to some tutorials that make this happen?

Asher11
  • 1,295
  • 2
  • 15
  • 31

1 Answers1

1

I'm preety sure a combination of JQuery and AJAX can do the trick. See: Update data on a page without refreshing and How to update data on field on a website from SQL database with AJAX and Django. I think the best example is in this SO question: setInterval and Ajax

Perhaps the following code snippet can be useful for you, which is supposed to be added in the html template.

<script type="text/javascript">
        $(function() {
            function callAjax(params, title, url){
                $.ajax({
                    #<<<here you build the request, the html-POST>>>
                    }
                });          
            };

            function regularCall() {
                callAjax("", "", "{% url 'the-url-that-you-expect-from-django-side' %}");
            };

            setInterval(regularCall, 10000); // Time in milliseconds
            callAjax("", "", "{% 'the-url-that-you-expect-from-django-side'  %}"); // First Call
        });

</script>
Community
  • 1
  • 1
eguaio
  • 3,754
  • 1
  • 24
  • 38
  • Very intresting stuff. Will look into it right away – Asher11 Dec 18 '16 at 12:36
  • Please, let me know if you were not able to solve your problem, or if it required some special tweaks. – eguaio Dec 18 '16 at 14:01
  • @eguaio hi! i am facing the same problem in my messaging app in django i have a problem of reloading page.i have searched many thing here but none of them work.could you please help me ? – Thierno Amadou Sow Feb 14 '21 at 09:10