Live Data Access Posting requests and handling data responses into live charts
Live Chart Updates with Asynchronous Data Updates
The charts page in the sadjdemo application shows how to link up a Django server via Ajax calls.
There are two examples. The first refreshes all the data into the chart with each call. The browser calls the Django server repeatedly, and receives back asynchronous data messages, each of which contains the full data-set for the chart. The second example adds to the existing data in the chart after each call, deleting older data points to keep a constant duration of displayed data.
sadjcore
functionality Using a Django form is the most reliable and straightforward method of posting data back to the server, in particular as it handles the CSRF token with a simple template-include. The sadjdemo frame contains a hidden form for this purpose, which can be used with a simple call to a generic method in the sadjcore javascript. There are more details about this on the ajax documentation page
In this case, the script on the page contains the following line:
sadj$.utils.ajaxPOST("api/chartData/", JSON.stringify(payload), "sadj_charts", demoLineUpdate)
The sadjdemo
Django app then contains the url definition:
path("api/chartData/", demo_views.chart_data)
alongside the code at demo_views.chart_data
which actually returns new data in response to the call.
The Django-side code tracks the last set of values sent, so each data sample is within a reasonable range of the previous one. Note that you do not need to be logged in for this to work, Django can track per-session variables for anonymous users using the session ID based on session cookies.