How to pass javascript variable to macros in jinja2 template

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

Above Javascript is written in jinja2 template. It is supposed to pass the value of the javascript variable (i.e. the variable name) for the python function in the macro. I know that the above code will not solve my goal, since I do not pass the value of the javascript variable correctly for the macro. Does anyone have a method to pass a javascript variable to a macro in a jinja2 template?

+4
source share
1 answer

javascript , , javascript. , name, javascript, , <script></script>.

. , , , . :

<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') }} . say py_fn - : , . py_fn ('some_string') 'some_string', , , "" :

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

, , js- script, js_fn window, - . , , js_fn some_string. , JS Python/Flask .

+2

Source: https://habr.com/ru/post/1605880/


All Articles