【问题标题】:Append json data to django table automatically自动将 json 数据附加到 django 表中
【发布时间】:2017-11-03 17:08:45
【问题描述】:

当一些异步任务用 celery 完成时,我会在我的 django 网站上收到 json 文件。

我想在可用时自动附加 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>

【问题讨论】:

  • 你应该为此尝试一些东西。如果你卡在某个地方,请来这里询问你的代码..

标签: python html json django


【解决方案1】:

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> 

【讨论】:

  • 作者说without refreshing the page,所以可能不符合他或她的要求。
  • 这更像是一种变通方法而不是解决方案,因为它根本不是用户友好的
【解决方案2】:

有两种选择:

1) 您可以使用 Django 频道,从技术上讲,您将使用websockets 推送新的更新。你可以在这里找到更多信息:https://channels.readthedocs.io/en/stable/

2) 第二种选择是让您的前端以时间间隔发出ajax 请求,例如 5 秒。这将调用一个 API 端点,并使用您将从那里获得的数据来更新您的前端。

希望对您有所帮助 - 如果您需要更多信息,我很乐意提供帮助。

【讨论】:

  • 我尝试使用 Ajax 将 json 解析到表中(请参阅我编辑的答案),但它不起作用。
  • 为什么不起作用?有什么问题,你有没有卡在任何地方?
【解决方案3】:

我认为在这里您应该使用 ajax 对 javascript 进行附加工作以获取最新数据并更改页面。
一旦模板被渲染并返回,就不能轻易更改。

【讨论】:

    猜你喜欢
    • 2017-09-22
    • 2016-04-27
    • 2016-08-14
    • 1970-01-01
    • 2013-03-31
    • 1970-01-01
    • 2021-07-07
    • 1970-01-01
    • 2016-12-08
    相关资源
    最近更新 更多