Python 如何在不刷新页面的情况下自动重新加载烧瓶中的jinja 2数据?
我试图在有新数据可用时立即将数据加载到json2模板中,而不刷新页面,但我无法做到这一点,这是我迄今为止尝试的主要功能Python 如何在不刷新页面的情况下自动重新加载烧瓶中的jinja 2数据?,python,jquery,html,python-3.x,jinja2,Python,Jquery,Html,Python 3.x,Jinja2,我试图在有新数据可用时立即将数据加载到json2模板中,而不刷新页面,但我无法做到这一点,这是我迄今为止尝试的主要功能 @app.route("/") def index1(): return render_template("index.html",gamest=get_games()) 返回信息的服务器上的终结点 @app.route("/sys_info.json") def index(): return get_games() jinja 2脚本: <script
@app.route("/")
def index1():
return render_template("index.html",gamest=get_games())
返回信息的服务器上的终结点
@app.route("/sys_info.json")
def index():
return get_games()
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>
{{index}}{#这是从根视图#传入的原始系统信息#
setInterval(function(){//将数据从端点加载到div
$(“#content”).load(“/sys_info.json”)
},1000)
{玩家%%中游戏的%
{%if游戏[“击球队]%}
{%endif%}
{{比赛[“击球队”]}{{比赛[“奔跑10”]}
{{游戏[“wickets10”]}
我只能在终端中看到值的变化,但我看不到网页数据的任何变化仍然是静态的,我如何修复此问题,以便在不刷新页面的情况下在网站中动态更改数据?请求数据,但js脚本不会刷新html。您必须为浏览器添加逻辑以加载html中的数据。例如:
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)
setInterval(函数(){
//将数据从端点加载到div中
$.getJSON(“/sys\u info.json”),函数(数据){
$.each(数据、函数(\元素){
$('.list group').append(''+elment['Batting team']+');
});
})
},1000)
您需要客户端浏览器来执行与使用jijna在服务器上相同的操作。感谢@mberacochea的回答,您能帮助我理解javascript代码吗?我无法检查此内容,因为目前没有可用的实时数据。我建议您阅读一些教程。MDN有很好的文档,这对我不起作用,setInterval(function(){//将数据从端点加载到div$(“#content”).load(“/sys#info.json”)。然后(function(data){$.each(data,function(#,element){$('.list group')。append('+elment[“runs10”]+';})}),1000)我看不到值的任何更改控制台上是否有错误?。你调试代码了吗?