简体   繁体   English

是否可以使用用户定义的JS变量字符串插入到Flask变量中?

[英]Is it possible to use a user-defined JS variable string to impute into a Flask Variable?

TL;DR --> can I pass JS var strings into {{ }} to retrieve Python vars from Flask app? TL; DR->是否可以将JS var字符串传递到{{}}中以从Flask应用程序检索Python var?

Instead of writing multiple iterations of a JS code block each with only slightly differently named Jinja variables (with similar conventions) like: 而不是编写JS代码块的多个迭代,每个迭代仅使用名称略有不同的Jinja变量(具有类似的约定),例如:

 //PTC Color Change
if ({{ptc_precip_probs[1]}} <= 25 && {{ptc_precip_probs[2]}} <= 25) {
    $("#ptc_block").css("background", "#21CE99"); //green
} else {
    $("#ptc_block").css("background", "#F45531"); //red
}

//COL Color Change
if ({{col_precip_probs[1]}} <= 25 && {{col_precip_probs[2]}} <= 25) {
    $("#col_block").css("background", "#21CE99"); //green
} else {
    $("#col_block").css("background", "#F45531"); //red
}enter code here

is it possible to do something like this: 是否可以做这样的事情:

var cities = ["nrg", "rrg", "gsm", "ptc", "col"];

for (var city in cities){
    var block = "."+cities[city]+"_block";
    var precipOne = String(cities[city])+"_precip_probs[1]";
    var precipTwo = String(cities[city])+"_precip_probs[2]";
    if ({{precipOne}} <= 25 && {{precipTwo}} <= 25) {
        $(String(block)).css("background", "#21CE99"); //green
    } else {
        $((block)).css("background", "#F45531"); //red
    }
}

When I attempt to do this exact operation, however I receive a Jinja2 error. 当我尝试执行此确切操作时,但是收到Jinja2错误。

No, because JS is client-side. 不,因为JS是客户端。 From http://jinja.pocoo.org/docs/2.10/templates/# 来自http://jinja.pocoo.org/docs/2.10/templates/#

A template contains variables and/or expressions, which get replaced with values when a template is rendered; 模板包含变量和/或表达式,在渲染模板时将其替换为值; and tags, which control the logic of the template. 和标签,它们控制模板的逻辑。 The template syntax is heavily inspired by Django and Python. 模板语法在很大程度上受到Django和Python的启发。

But if you want a variable from your flask app, you can pass it to the render_template function as demo'd in the question asked here Include html file in Jinja2 template 但是,如果您希望从flask应用程序中获取一个变量,则可以按照此处提出的问题的演示将其传递给render_template函数, 在Jinja2模板中包含html文件

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM