Search code examples
javascriptflaskjinja2

Jinja templating in JavaScript


I am using Flask, and I am trying to send data to my HTML page. The obvious choice is Jinja templating: {{ input_msg }}, but the problem is that I am inside curly brackets already, since I need to put the variable into JavaScript:

request.onload = function() {
if (request.status === 200 && request.responseText === 'done') {
    window.location = {{ inp_msg }};
}
};

The above will not work because curly brackets are being used for templating, but inside JavaScript as well. Is there another way to send data, not Jinja? If not, is there another Jinja way?


Solution

  • You have to pass a string for window.location.

    window.location = '{{ inp_msg }}'
    

    Also use can use url_for function.

    window.location = '{{ url_for(func_name) }}'