admin管理员组文章数量:1310143
<script>
function myFunction() {
var name = "some_string";
var display = "{{ python_function(name) }}";
alert(display);
}
</script>
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?
<script>
function myFunction() {
var name = "some_string";
var display = "{{ python_function(name) }}";
alert(display);
}
</script>
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?
Share Improve this question edited Nov 19, 2020 at 19:40 davidism 127k31 gold badges415 silver badges347 bronze badges asked Sep 4, 2015 at 5:12 RohanilRohanil 1,8875 gold badges27 silver badges51 bronze badges1 Answer
Reset to default 6You 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>
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>
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.
本文标签: pythonHow to pass JavaScript variable to function in Jinja tagStack Overflow
版权声明:本文标题:python - How to pass JavaScript variable to function in Jinja tag - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741834278a2400119.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论