当一些asyncronous任务完成芹菜时,我收到json文件到我的django网站。
我希望在可用时自动附加json数据(不刷新页面)。
任何想法?
谢谢,
我试图直接将json解析到表中但它不起作用。
<table id="myTable" style="width:90%;font-style: normal;">
<thead>
<tr style="font-style: italic;">
<th><div class="th-inner"> Proc. Started </div></th>
<th><div class="th-inner"> Proc. Finished </div></th>
<th><div class="th-inner">Result </div></th>
</tr>
</thead>
<tbody>
{% for document in images %}
<tr>
<td>{{ document.image.date_image_upload }}</td>
<td> </td>
<td id=jsonResultDiv> <h3 style="color: #FFFFFF;">Results will appear here...</h3> </td>
<script type="text/javascript">
var DEV_URL = "{{ document.remote_processed_json }}";
var resultJsonUrl = DEV_URL;
function loadJSON(callback) {
var xobj = new XMLHttpRequest();
xobj.overrideMimeType("application/json");
xobj.open('GET', DEV_URL, true); // Replace 'my_data' with the path to your file
xobj.onreadystatechange = function () {
if (xobj.readyState == 4 && xobj.status == "200") {
// Required use of an anonymous callback as .open will NOT return a value but simply returns undefined in asynchronous mode
callback(xobj.responseText);
}
};
xobj.send(null);
}
loadJSON(function (response) {
// Parse JSON string into object
actual_JSON = JSON.parse(response);
var element = document.getElementById("jsonResultDiv");
element.innerHTML = "<h3>screening result:<h3>";
if (actual_JSON[0]['evaluation'] == 0)
element.innerHTML += '<h3 style="color: #66FF66;">Healthy.</h3>';
else if (actual_JSON[0]['evaluation'] == 1)
element.innerHTML += '<h3 style="color: #FF6666;">' + actual_JSON[0]['annotations'].length + ' micro found.</h3>';
else element.innerHTML += '<h3 style="color: #FFF000;">Problem parsing results file.</h3>';
});
</script>
</tr>
{% endfor %}
</tbody>
</table>
答案 0 :(得分:0)
像HTML一样简单,每隔30秒左右自动刷新一次页面就可以使用已安装的解决方案吗?如果是,请参阅https://www.w3schools.com/tags/att_meta_http_equiv.asp:
Refresh document every 30 seconds:
<head>
<meta http-equiv="refresh" content="30">
</head>
答案 1 :(得分:0)
有两种选择:
1)您可以使用Django频道,从技术上讲,您将使用websockets
推送新的更新。您可以在此处找到有关它的更多信息:https://channels.readthedocs.io/en/stable/
2)第二个选项是使你的前端发出ajax
个请求的时间间隔,例如5秒。这将调用API端点,并使用您将从那里获得的数据,更新您的前端。
希望有所帮助 - 如果您需要更多信息,我将很乐意为您提供帮助。
答案 2 :(得分:0)
我想在这里你应该使用ajax在javascript上添加工作来获取最新数据并更改页面 渲染并返回模板后,无法轻松更改。