I'm experienced with Python but fairly new to web development.
Basically, I have a Django app with a bunch of data that generates a webpage. I'd like to generate some interactive charts using Javascript. The problem: I need to get my data from the Django models into my static folder JS code. I'm unsure of the "best" way to do this, and was looking for some insight, or even just a tutorial or something that explains how to do it.
#Passing Django data to Javascript
1 messages · Page 1 of 1 (latest)
Getting it to static folder kills the idea of static.
You can feed data into template instead and read it with JS.
Or make JS send a separate request to django directly
e.g. in template you write data to DOM elements with data-* attrs as just values or encoded as json
Or you could have a <script> block where you render data already as js-ready format
@unreal summit I've done it in my app just standard CBV views but overwritten the get context data function. Just a context["chart"]. Get the data I want convert to json and in the template just use the template tag to json it as JSON.
I eventually figured it out and may as well put it here for anyone who has the same sort of question. I didn't want my data to be in the template itself. Long story short you can send your data to a url in views.py (I used a different url as my index page), and then use javascript fetch API to get the data from that url