I'm having a difficult time passing variables from the python backend to javascript. A lot of my variables look like this in javascript:
if ('{{ user.has_paid_plan}}' == 'True') {
isPayingUser = true;
} else {
isPayingUser = false;
}
It's ugly and I'm sure there's a much cleaner way to do this. How should this be done?
var has_paid_plan = {{ user.has_paid_plan }}. Then use that js variable everywhere in rest of your js. This way you can also use these variables in external js files too. Other option is to use data attribute. You can have a data-has_plan in some div or anything. Then you can get its value using js/jQuery. I go with first option when I wil have only one user on a page, if I am showing a list of users, I will go with data attributes{{ user.has_paid_plan }}, I get an error in javascript, because it is literally passed asTrue(string, no quotes).true, or use quotes and check for"True"or"False"var foo = "{{ foo }}";then.