Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
96
rated 0 times [  98] [ 2]  / answers: 1 / hits: 69923  / 11 Years ago, fri, february 7, 2014, 12:00:00

How do i pass jinja2 data into javascript.
I have a Flask REST url as /logs/<test_case_name>
I am trying use .getJSON() to query the above URL and hence would want to pass the jinja2 data which has the testcasename to .getJSON function.



sample code:



<script type=text/javascript>
alert({{name}});
</script>


It doesn't work.
Any suggestions please?


More From » python

 Answers
12

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


[#72673] Wednesday, February 5, 2014, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
bobbyallanh

Total Points: 693
Total Questions: 120
Total Answers: 101

Location: Bermuda
Member since Thu, Apr 20, 2023
1 Year ago
;