Javascript无法识别Flask变量 [英] Javascript is not recognizing a Flask variable

查看:59
本文介绍了Javascript无法识别Flask变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将一组变量传递到Flask模板中,我想首先使用Javascript操作它们.问题是当我使用 {{var}} 语法时,Javascript无法识别它.错误看起来像这样.左括号给出了标识符或字符串文字或数字文字预期"错误,并且变量名给出"Expression语句未赋值或未调用"错误.

I'm passing a set of variables into a Flask template, and I would like to first manipulate them with Javascript. The problem is that when I use the {{ var }} syntax, Javascript isn't recognizing it. The errors look like this. The left brackets give an "Identifier or string literal or numeric literal expected" error, and the variable names give an "Expression statement is not assignment or call" error.

稍后,我会在文档的HTML部分中使用 {{var}} 语法,当我这样做时,它们看起来就很好.另外,像我对其他变量所做的那样,将其括在引号中也不起作用.有人知道这个问题可能是什么吗?谢谢.

I use {{ var }} syntax later, within the HTML portion of the document, and when I do that they appear just fine. Also, enclosing it in quotes as I do for a different variable doesn't work either. Anyone know what the issue could be? Thanks.

推荐答案

Jinja2(烧瓶的模板引擎)是一个预处理器,这意味着它的输出是真正的JS,它并不关心您是否将其与HTML,JS一起使用等等,它只会打印文本.

Jinja2 (flask's templating engine) is a preprocessor, meaning that its output is the real JS and it does not care about you're using it with HTML, JS or whatever, it only prints text.

您遇到的错误是您的文本编辑器试图为您提供帮助,但这还不足以使您意识到您正在编写Jinja2而不是javascript.

That error you're getting is your text editor trying to help you but it's not smart enough to realize you're writing Jinja2 instead of javascript.

编辑:此外,正如@davidism所说,您必须使用jinja2块.

Edit: also, as @davidism says, you have to use jinja2 blocks.

这篇关于Javascript无法识别Flask变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆