Friday, May 10, 2024
 Popular · Latest · Hot · Upcoming
64
rated 0 times [  65] [ 1]  / answers: 1 / hits: 15068  / 8 Years ago, mon, april 18, 2016, 12:00:00

I know that we can use Django variable in templates(html files) with {{variable}}, but how can I use a variable Django in a Javascript file that included in the template?



For example here is my template.html:



<html>
<head>
<script src=/static/youtube.js></script>
...


how can I use variable {{user.get_username}} in youtube.js? not in template.html cause I did't wrote my script here...and when I use {{user.get_username}} in Javascript file youtube.js it caused an error invalid token { , but in the template it works fine.



Thanks a lot!


More From » python

 Answers
5

You need to print it before your script tag



<html>
<head>
<script>
var username = {{user.get_username}};
</script>
<script src=/static/youtube.js></script>
...


Then you can use the username variable inside your youtube.js as it is declared as a javascript global variable.


[#62507] Friday, April 15, 2016, 8 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
maurodannyr

Total Points: 126
Total Questions: 103
Total Answers: 105

Location: Maldives
Member since Sun, Feb 27, 2022
2 Years ago
;