[英]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.