Unable to pass jinja2 variables into javascript snippet Unable to pass jinja2 variables into javascript snippet flask flask

Unable to pass jinja2 variables into javascript snippet


other than encapsulating the variable in a string, an alternate is jquery for profit:

its generally a bad idea to mix template language with javascript. An alternative would be to use html as a proxy - store the name in an element like so

<meta id="my-data" data-name="{{name}}" data-other="{{other}}">

then in the javascript do

var djangoData = $('#my-data').data();

this has advantage in:

  • javascript no longer tied to the .html page
  • jquery coerces data implicitly


Try with quotes:

alert("{{name}}");


I know this is a kinda old topic, but since it attracts a lot of views I suppose some people are still looking for an answer.

Here's the simplest way to do it:

var name = {{name|tojson}};

It will 'parse' whatever 'name' is and display it correctly:https://sopython.com/canon/93/render-string-in-jinja-to-javascript-variable/