Home > OS >  How to send data from view to template continously in Django
How to send data from view to template continously in Django

Time:09-16

I need to send data from my view or my model database to my HTML/javascript template. What technology or methods should I use for that? I can't simply use for example

return render(request, "check_by_callsign.html", {"latitude": latitude, "longitude": longitude})

because that would only mean one return of data.

CodePudding user response:

It's not that easy to implement websockets in Django (async programming). This is being worked on, in the meantime look at "Django Channels".

To poll your view every x second you can use the Fetch API within Javascript:

const myDiv = document.getElementById('coordinates')

function fetchCoordinates() {
  fetch('yourURLhere')
    .then((response) => response.text())
    .then((data) => myDiv.innerHTML = data)
    }

window.addEventListener('load', event => {
  let fetchInterval = 5000; // 5 sec, 10000 = 10 sec
  setInterval(fetchCoordinates, fetchInterval);
})
  • Related