JS 通过 flask {{ variable }} 格式访问 python 变量不起作用

蟒:


import json

posts = [

{'author':'JL Rowling','title':'Harry Potter'},

{'author':'JRR Tolkien','title':'Lord of the Rings'},

]

encoded_posts = json.dumps(posts)

javascript:


let obj = {{ encoded_posts }};

控制台中记录了 JS 错误:


Uncaught SyntaxError: Unexpected token ';'

这是根据控制台的页面。它似乎正在失去(?)的.{{encoded_posts}}


<!DOCTYPE html>

<html>

  <head>


      <title>Flask Blog - About</title>


  </head>

  <body>

    <h1>About page!</h1>

    <script>

      let text = document.getElementsByTagName('h1')[0];

      text.addEventListener('click',function(){

        text.style.color = 'red';

      });


      let obj = ;

      let jsonPosts = JSON.parse();

      console.log(jsonPosts);


    </script>

  </body>

</html>

发生了什么事情?


摇曳的蔷薇
浏览 129回答 1
1回答

慕尼黑5688855

你有没有试过做:let&nbsp;obj&nbsp;=&nbsp;JSON.parse('{{&nbsp;encoded_posts&nbsp;}}');
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript