Greetings.
I want to make an asynchronous update of the data on the page. There is a table that loads data from the database. Outputs via Django template engine, data in the table is added/updated from the side.
And so that I can see them in the table, I need to refresh the page itself. But I need the table to be updated without updating the page itself. I reviewed a lot of options and examples on the Internet, but I did not understand. There are no simple examples.
Please help, I think many people are interested in this question
view.py
def test(request):
last_3 = New_tables.objects.filter(active=0).order_by('-id')[:3]
last_new_table = reversed(last_3)
return render(request, 'main/test.html', {"last_new_table":last_new_table})
test.html
That’s how I tried to output via Javascript, but unfortunately it doesn’t work either. Updates the data on the page only after the page is refreshed…
{% extends 'main/base.html' %} {%block content%} {% load static %}
<script src="https://snipp.ru/cdn/jquery/2.1.1/jquery.min.js"></script>
<table class="table table-hover table-dark">
<thead>
<tr>
<th class="text-white-50 bg-dark" style="font-size: 0.9em;" scope="col">Table</th>
<th class="text-white-50 bg-dark text-center " style="font-size: 0.9em;" scope="col">N</th>
<th class="text-white-50 bg-dark text-center " style="font-size: 0.9em;" scope="col">Type</th>
<th class="text-white-50 bg-dark text-center " style="font-size: 0.9em;" scope="col">Quote</th>
<th class="text-white-50 bg-dark text-center " style="font-size: 0.9em;" scope="col">Start</th>
</tr>
</thead>
<tbody class="tbody">
</tbody>
</table>
<script>
function index() {
$('.tbody').empty()
{% for kl in last_new_table %}
$(`
<tr>
<th class="text-white-50 bg-dark" scope="row">TBL000${"{{kl.id}}"}</th>
<td class="text-warning bg-dark">${"{{kl.n}}"}</td>
<td class="text-info bg-dark text-center">${"{{kl.instrument}}"}</td>
<td class="text-info bg-dark text-center"><img src='{% static "down.png" %}' width="20%" height="15%"></td>
<td class="text-center" style="font-size: 0.9em;">....</td>
</tr>
`
).appendTo('.tbody')
{% endfor %}
}
setInterval(index, 200)
</script>
{%endblock content%}
how to make the data automatically loaded into the table?