python代码:
from flask import render_template
from app import app
@app.route('/')
@app.route('/index')
def index():
    user = {'nickname': 'Miguel'}  # fake user
    posts = [  # fake array of posts
        {
            'author': {'nickname': 'John'},
            'body': 'Beautiful day in Portland!'
        },
        {
            'author': {'nickname': 'Susan'},
            'body': 'The Avengers movie was so cool!'
        }
    ]
    return render_template("index.html",titlt="home",posts=posts,user=user)


前端javascript代码:


<script language="JavaScript"> $(document).ready(function () { var s='{{ posts|tojson|safe}}'; alert(s); });</script>

flask模板中使用 jaavscript接收模板传递的值
在接收java传递的值时,使用

var s='${posts}';

即可,注意是单引号,双引号可能取不到值。

相关文章:

  • 2021-09-11
  • 2022-01-30
  • 2021-11-30
  • 2022-12-23
  • 2021-12-04
  • 2022-12-23
  • 2021-11-18
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-01-23
  • 2022-02-11
  • 2021-12-22
  • 2021-11-01
相关资源
相似解决方案