【问题标题】:Jquery .load for update fragment on flask\pythonJquery .load 用于烧瓶\python 上的更新片段
【发布时间】:2018-06-01 13:10:13
【问题描述】:

我正在尝试在我的 Flash 应用程序中使用 jquery .load 来更新 div,但这不起作用。

routes.py

@app.route('/index', methods=['GET', 'POST'])
@app.route('/', methods=['GET', 'POST'])
def red():  
    return redirect('/index.html')

@app.route('/index.html', methods=['GET', 'POST'])
def index():
    return render_template('index.html', title='Estat')

index.html

 <script>
     function run() {
    $( "#display" ).load( "/index.html #display" );
    }

    setInterval(run, 1000);
    </script>

<div id="display"><h1>word 2</h1></div> 

我希望每次在某个时间间隔内更改模板文件时 div 块的内容都会更改。请帮帮我。

【问题讨论】:

  • 该脚本在没有flask+python的情况下工作正常。我认为问题出在 index.html
  • 您能否将我的回答标记为已接受,以便我知道我已成功回答您的问题

标签: javascript python jquery html flask


【解决方案1】:

好的,你在正确的轨道上。 div 应该正确重新加载,如果不尝试将其包装在添加 body 和 head 标签中,这对我有一次帮助。现在由您决定将您想要的任何数据返回到网页。假设您要显示时间: 在 python 中使用:import datetime 导入日期时间模块 并使其成为索引函数:

def index():
    return render_template('index.html', title='Estat', time = str(datetime.datetime.now()))

然后将您的 div 修改为: &lt;div id="display"&gt;&lt;h1&gt;{{ time}}&lt;/h1&gt;&lt;/div&gt;

现在您的网页应该返回时间,并且每秒刷新一次

【讨论】:

  • 记住,试着用适当的 body 和 head 标签包裹 html ;例如它应该看起来像 ;`JAVASCRIPT HEREDIV HERE
  • 它适用于变量,但不适用于模板文件的更改。例如,我在记事本中更改

    Time {{ time}}

    上的文本并保存。我没有重新启动服务器,站点需要查看更改,但这不会发生
  • 我认为这都是关于在关闭调试模式的情况下缓存模板
  • 是的,我认为您需要重新启动服务器才能看到模板更改,否则使用调试模式
猜你喜欢
  • 2014-04-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多