I have an API key declared in settings.py as a variable.
I need that API key in a call to an API which takes place in a JS file.
How can I access that settings variable in the JS file?
What is a secure way to do this?
Thank you
The generally-accepted “safe” method for injecting Django data into a template for access by a JavaScript function is by using the json_script filter.
But I’m not sure what you mean by secure. If you’re looking to prevent someone from looking at the source for your page and finding your API key, then you need to take a different approach.
Thank you, I would use the json_script filter.
The issue that I have now is how to import the variable from settings.py into the template.
Like all other data being rendered in the template, it gets passed to the rendering engine through the context.
Import the settings in the view and add that setting to the context. It is then available to be rendered by the template.