简体   繁体   English

如何在不刷新页面的情况下自动重装Flask中的Jinja 2数据?

[英]How to do auto-reloading of jinja 2 data in Flask without refreshing page?

I'm trying to load the data into json2 template as soon as there is new data available without refreshing the page but I'm unable to do that, this is what I tried till now main Function 我试图在没有可用新数据的情况下尽快将数据加载到json2模板中,而不刷新页面,但是我无法做到这一点,这是我到目前为止尝试过的功能

@app.route("/")
def index1():
return render_template("index.html",gamest=get_games())

endpoint on the server that returns info. 返回信息的服务器上的端点。

@app.route("/sys_info.json")
def index():
     return get_games()

jinja 2 scripts: jinja 2脚本:

<script src="https://code.jquery.com/jquery-3.2.1.js"></script>
<div id="content">{{ index }}</div> {# this is the original system_info passed in from the root view #}

<script>
setInterval(function(){ // load the data from your endpoint into the div
    $("#content").load("/sys_info.json")
},1000)
</script>

<div class="list-group">
            {% for  game in gamest %}
            <a class="score-size text-xs-center nounderline list-group-item list-group-item-action" >
               <div class="row">
                  <div class="col-xs-4">
                     {% if game["Batting_team_img"] %}
                     <img class="team-logo" src="/static/{{ game["Batting_team_img"] }}">
                     {% endif %}
                     {{ game["Batting team"] }}  {{ game["runs10"] }}
                     </b>
                     <br>
                     {{ game["wickets10"] }}
                  </div>

I can see only change in the value in terminal but I can't see any changes in webpage data remains static, how can I fix this so data are changed dynamically in the website without refreshing page? 我只能在终端中看到值的更改,但是看不到网页数据中的任何更改保持静态,如何解决此问题,以便在网站中动态更改数据而无需刷新页面?

The data is requested but the html is not refreshed by the js script. 请求了数据,但是js脚本未刷新html。 You have to add the logic for the browser to load the data in the html. 您必须为浏览器添加逻辑以将数据加载到html中。 For example: 例如:

setInterval(function() {
    // load the data from your endpoint into the div
    $.getJSON("/sys_info.json", function (data) {
       $.each(data, function (_, element) {
         $('.list-group').append('<div>' + elment['Batting team'] + '</div>);
       });
    })
},1000)

You need the client browser to do the same you do on the server with jijna. 您需要客户端浏览器来执行与jijna在服务器上相同的操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 基于时间的自动重新加载Python / Flask中的Jinja元素,而无需刷新页面 - Time based auto-reloading of Jinja element in Python/Flask without refreshing page 新 CLI 中的烧瓶和自动重新加载 - Flask and auto-reloading in new CLI Django 如何在不刷新或重新加载页面的情况下插入数据 - Django How do i Insert data without refreshing or reloading page 如何在不刷新 flask 页面的情况下获取用户输入? - how do I take user input without refreshing the page in flask? 为什么我的 Docker Volume 不允许自动重新加载我的 Flask 应用程序? - Why does my Docker Volume not allow for Auto-Reloading my Flask Application? lighttpd django fastcgi和自动重新加载? - lighttpd django fastcgi and auto-reloading? 在html页面中重新加载表单而不通过渲染模板烧瓶刷新整个页面 - Reloading form in html page without refreshing the whole page through render template flask 如何在不刷新 Flask 项目的页面的情况下更新值? - How do I update values without refreshing the page on my Flask project? Flask 如何在不渲染新页面的情况下只为 jinja 返回一个变量? - Flask how to return just a variable for jinja without rendering a new page? 如何在不重新加载 Flask 页面的情况下显示闪烁的消息? - How to display flashing message without reloading the page in Flask?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM