How to pass DJANGO (Python) variable to javascript

Asked

Viewed 141 times

0

People. I have a problem where a friend is backend in DJANGO and I frontend in Javascript. I would like to know how to pass a variable from a Django array to Javascript.

I’m trying to create a dynamic table. So far searching in Stackoverflow, I was able to pass a numeric variable to html, and then get its value in javascript. But it was a gambit. It was something like:

<input type="hidden" id="ordersSold" value="{{ sold }}">

And then I catch her with:

var cardBodyOrders = document.getElementById("totalOrders").value;

But that’s gambiarra. Would there be some way I could take this variable {{ sold }} and turn it into an array of Javascripit objects?

var cardBodyOrders = [ {name: ... }, {email: ... }, ]

Etc.. or something like that.

  • Hi, is your friend injecting the variables in the template or are you using API?

  • I think in the template. I’ll check with him.

  • The js code is in the same html file? Why could you just do this: var cardBodyOrders = {{ sold }}.

  • Um... no. It’s in a separate Js. Cool. I’ll try. Thank you. But taking advantage to complicate more, and if I want to continue using in a separate Js, as I proceed?

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.