如何将JavaScript变量传递给Jinja标签 [英] How to pass JavaScript variable to function in Jinja tag

查看:93
本文介绍了如何将JavaScript变量传递给Jinja标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<script>
       function myFunction() {
                var name = "some_string";
                var display = "{{ python_function(name) }}";
                alert(display);
       }
</script>

以上Javascript是在jinja2模板中编写的.它应该将javascript变量(即var名称)值传递给宏中的python函数.我知道上面的代码无法解决我的目的,因为我没有正确将javascript变量值传递给宏.是否有人在jinja2模板中将javascript变量传递给宏的方法?

Above Javascript is writen in jinja2 template. It is supposed to pass javascript variable (i.e. var name) value to python function in macro. I know above code won't solve my purpose as I am not passing javascript variable value correctly to macro. Does anybody have method on passing javascript variable to macro in jinja2 template?

推荐答案

您不能以这种方式将值从javascript传递到模板,因为模板将在响应返回浏览器之前呈现,以供javascript引擎评估.模板渲染器能够解析javascript代码中指定的name值的唯一方法是解释嵌入在<script></script>中的字符串.

You cannot pass values from javascript to the template that way because the template is going to be rendered before the response goes back to the browser for the javascript engine to evaluate. The only way the template renderer would be able to resolve the value of name specified in the javascript code would be to interpret the string embedded in <script></script>.

更新.让我们看看您的第二次尝试,您说的成功了.你有:

Update. Let's look at your second attempt, the one that you say has worked. You have:

<body>
    <button onclick="js_fn('{{ py_fn('some_string') }}')">Click me</button>
    <script>
        function js_fn(variable) {
            alert(variable);
        }
    </script>
</body>

大概是部分原因(例如 _index.html ).范围为py_fn的视图将加载 _index.html ,评估字符串"py_fn('some_string')",并用该评估结果替换{{ py_fn('some_string') }}.假设py_fn是字符串上的标识函数:它是一元函数,接受字符串并立即返回它.然后,评估"py_fn('some_string')"的结果将是字符串'some_string',它将被替换回以获得最终的所谓的渲染"模板:

Presumably this is in some partial (say _index.html). A view that has py_fn in scope, loads _index.html, evaluates the string "py_fn('some_string')", and replaces {{ py_fn('some_string') }} with the result of that evaluation. Let's say py_fn is the identity function on strings: it's a unary function that takes a string and immediately returns it. Then, the result of evaluating "py_fn('some_string')" will be the string 'some_string', which will be substituted back, obtaining the final, so-called "rendered" template:

<body>
    <button onclick="js_fn('some_string')">Click me</button>
    <script>
        function js_fn(variable) {
            alert(variable);
        }
    </script>
</body>

此字符串将成为请求响应主体的一部分,因此浏览器将在窗口上转储按钮,评估脚本块内的js代码,这将在window上创建全局变量js_fn ,它将采取一些措施并对其发出警报.单击该按钮时,将始终使用常量some_string调用js_fn.如您所见,没有值从JS传递到Python/Flask.

This string will be part of the response body of the request, so the browser will dump the button on the window, evaluate the js code inside the script block, which will create a global variable js_fn on the window, which will take something and alert it. The button, when clicked on, will call js_fn with the constant some_string, always. As you can see, there is no passing of values from JS to Python/Flask.

这篇关于如何将JavaScript变量传递给Jinja标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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