Trying to build a web application for the first time using Django. The table UI from the below script is rendering the data for the first time. But the Ajax call doesn't seem to call the get_more_tables in views.py and refresh the table data.
I have already looked at but didn't help my case Reload table data in Django without refreshing the page
home.html
{% extends "templates_dashboard/base.html" %}
{% block content %}
<head>
<script src="https://code.jquery.com/jquery-3.2.1.min.js" type="text/javascript"></script> <!-- ADD THE CLOSING TAG HERE TOO! -->
</head>
<td class="more-padding-on-right">CURRENT RESPONSE TIME OF WEBSITES</td>
<table id="_response_time" class="table table-striped table-condensed">
<tr>
<th>Website</th>
<th>Current Response Time(ms)</th>
</tr>
{% for posts in posts2 %}
<td>{{ posts.website }}</td>
<td>{{ posts.response_time}}</td>
</tr>
{% endfor %}
</table>
<script> // <-- ADD THIS!
$(document).ready(function () {
var append_increment = 0;
setInterval(function() {
$.ajax({
type: "GET",
url: "{% url 'get_more_tables' %}",
data: {'append_increment': append_increment}
})
.done(function(response) {
$('#_response_time').append(response);
append_increment += 10;
});
}, 10000)
})
</script>
{% endblock content %}
get_more_tables.html
{% load static %}
{% for post in posts %}
<tr>
<td>{{ post.website}}</td>
<td>{{ post.response_time }}</td>
</tr>
{% endfor %}
urls.py
urlpatterns = [
path('', views.home , name='dashboard-home'),
path('about/',views.about, name='dashboard-about'),
path('get_more_tables', views.get_more_tables , name='get_more_tables'),
]
views.py
def get_more_tables(request):
logger.info('************************************************** Refreshing Table Content')
increment = int(request.GET['append_increment'])
increment_to = increment + 10
my_response = responseTime()
return render(request, 'templates_dashboard/get_more_tables.html', {'posts': my_response[increment:increment_to]})
I'm expecting the table data for website and Response time to refresh every 10 seconds by printing the logger message in get_more_tables. I cannot see the table data getting refreshed or the logger message in the logs which made me think that the get_more_tables function is not invoked. Can someone help me understand why the get_more_tables function in views.py is not called?