javascript - JS通过烧瓶{{变量}}格式访问python变量不起作用
问题描述
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>
发生了什么?
解决方案
您是否尝试过这样做:
let obj = JSON.parse('{{ encoded_posts }}');
推荐阅读
- pika - 使用 pika.BlockingConnection 我想从队列中消费消息然后退出
- php - Style.css 文件目录
- c - 当节点已经存在时,代码实际上如何插入节点
- c++ - 我的项目中不能包含 boost 头文件 mapped_file.hpp
- html - 为什么每行只显示 3 个块而不是 4 个?
- git - 命令“git添加
" 将所有文件添加到舞台 - macos - Vim/Terminal 配色方案出现在 SSH 上,但不在本地 Mac 上
- kibana - Kibana 在 CentOS7 Core 中每 14 秒重启一次
- google-apps-script - 在谷歌表格中 - 如何应用 onEdit 触发器以仅在一张表格上的一个单元格上运行?
- python - 如何将多个图像从一个文件夹移动到python中的另一个文件夹?