提问人:Erik Isusquiza 提问时间:5/22/2017 最后编辑:Erik Isusquiza 更新时间:7/22/2022 访问量:1416
当有新数据可用时自动刷新 django 表
Refresh django table automatically when new data is available
问:
我已经实现了fetch_results函数,以便将新数据添加到 django 网站中的数据库中。此功能在每次页面刷新时激活。
views.py
def fetch_results():
# global result
documents = DRSDocument.objects.all()
for doc in documents:
computed_image_path = os.path.join(WORKING_DIRECTORY, doc.uuid, 'process_result.jpg')
drs_status = doc.status
if os.path.isfile(computed_image_path):
if drs_status == 'Q':
doc.date_process_end = timezone.now()
doc.status = 'F'
doc.save()
return render('list_admin.html', {'status': drs_status})
当一个图像在服务器中可用时,数据在 django 网页中可用。os.path.isfile(computed_image_path)
list_admin.html
<script>
function refresh() {
$.ajax({
url: '{% url table_monitoring %}',
success: function(data) {
var dtr = $("#container2c", data);
$('#container2c').html(dtr);
}
});
setTimeout("refresh()", 3000);
}
$(function(){
refresh();
});
</script>
{% endblock %}
{% block content %}
<div id="container2c" align="center">
<BR>
<BR>
{% if drs_images %}
<BR>
<form method="post" action="{% url 'retinal_drs_delete_multiple' %}" style="height: 530px;overflow-y: scroll;width: 90%;">
{% csrf_token %}
<table id="myTable" style="width:90%;font-style: normal;">
<thead>
<tr style="font-style: italic;">
<th><div class="th-inner"><IMG title="Click to select all" width=12px src="{% static 'images/red-delete-button.png' %}" onclick="return selectAllForDeletion();"></IMG></div></th>
<th><div class="th-inner"> Image </div></th>
<th><div class="th-inner"> # </div></th>
</tr>
</thead>
<tbody>
{% include 'list_admin_table.html' %}
</tbody>
</table>
<BR>
<BR>
<input type="submit" value="Delete selected">
<BR>
</form>
这是我list_admin_table.html
list_admin_table.html
{% for document in drs_images %}
<tr {% if document.completed %}style="color:white; font-weight:bold;"{% else %}style="color: black;"{% endif %}>
<td><INPUT type="checkbox" name="delete_{{ document.drs_image.uuid }}" id="id_delete_{{ document.drs_image.uuid }}" value="true"/></td>
<td><IMG WIDTH="22px" SRC="{% static 'images/' %}{{ document.drs_image.get_status_icon }}" title="{{ document.drs_image.get_status_display }}"> </td>
<td>{{ forloop.counter }}</td>
</tr>
{% endfor %}
这是我 urls.py
urlpatterns = [
url(r'^list/$', retinal_drs_views.list_admin, name='retinal_drs_list_admin')
url(r'^table-monitoring/$', retinal_drs_views.fetch_results, name="table_monitoring"),
]
我的目标是定期(每 10 秒)获取结果,如果它返回为 true,则自动刷新。table id="myTable"
我一直在阅读有关 AJAX、Jquery、Websockets 的信息......但我是 django 和客户端编程的新手,所以我没有得到任何有效的结果。
谢谢!
评论