# passing Django context data to an external .js file

**URL:** <https://forum.djangoproject.com/t/passing-django-context-data-to-an-external-js-file/3844>\
**Category:** Templates & Frontend\
**Created:** [August 12, 2020, 12:17am UTC](https://forum.djangoproject.com/t/passing-django-context-data-to-an-external-js-file/3844 "2020-08-12T00:17:28Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![kalistos1](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kalistos1/32/1843_2.png) [@kalistos1](https://forum.djangoproject.com/u/kalistos1)\
**Post date:** [August 12, 2020, 12:17am UTC](https://forum.djangoproject.com/t/passing-django-context-data-to-an-external-js-file/3844/1 "2020-08-12T00:17:28Z")

</div>

hello friend which way is the best way to pass a Django’s views.py context data into an external javascript file, so as to run the js function.  
for example i pass the value ‘product\_price’ into a context then display it in a html page using the jinja tag.  
now, what way is the best way to pass it to an external js file which has been included in the base .html  
i read up somethings where [json\_script](https://docs.djangoproject.com/en/dev/ref/templates/builtins/#std:templatefilter-json_script) was suggested and another where the jinja tag was suggested. so i decided to ask the pro. which way should i do it

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [August 12, 2020, 12:29am UTC](https://forum.djangoproject.com/t/passing-django-context-data-to-an-external-js-file/3844/2 "2020-08-12T00:29:25Z")

</div>

Are you talking about needing to do this in the initial rendering? Or are you talking about doing this as the result of an AJAX call?

If the first, can you be more specific about what it is you’re trying to achieve? Are you actually using Jinja in Django? Or are you using the Django template language?

I know the json\_script filter works extremely well, I use it myself.

But this is one of those “there’s no real ‘right’ or ‘wrong’ answer here, it’s whatever you’re comfortable with” type of situations.

Go with whatever fits your mindset and looks the most readable to you.

Ken

---

<div class="post-metadata">

**Author:** ![adamchainz](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/adamchainz/32/26_2.png) [@adamchainz](https://forum.djangoproject.com/u/adamchainz)\
**Post date:** [August 12, 2020, 9:34am UTC](https://forum.djangoproject.com/t/passing-django-context-data-to-an-external-js-file/3844/3 "2020-08-12T09:34:16Z")

</div>

On json\_script, my blog post covers more information: [https://adamj.eu/tech/2020/02/18/safely-including-data-for-javascript-in-a-django-template/](https://adamj.eu/tech/2020/02/18/safely-including-data-for-javascript-in-a-django-template/)

There are _some_ “wrong” ways that add security vulnerabilities 😉

---

<div class="post-metadata">

**Author:** ![kalistos1](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kalistos1/32/1843_2.png) [@kalistos1](https://forum.djangoproject.com/u/kalistos1)\
**Post date:** [August 12, 2020, 11:24pm UTC](https://forum.djangoproject.com/t/passing-django-context-data-to-an-external-js-file/3844/4 "2020-08-12T23:24:07Z")

</div>

hi thanks for your time ken

---

<div class="post-metadata">

**Author:** ![kalistos1](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kalistos1/32/1843_2.png) [@kalistos1](https://forum.djangoproject.com/u/kalistos1)\
**Post date:** [August 12, 2020, 11:25pm UTC](https://forum.djangoproject.com/t/passing-django-context-data-to-an-external-js-file/3844/5 "2020-08-12T23:25:17Z")

</div>

great article. thank you, i hope it works or me

---

<div class="post-metadata">

**Author:** ![Kamalabot](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kamalabot/32/14983_2.png) [@Kamalabot](https://forum.djangoproject.com/u/Kamalabot)\
**Post date:** [August 8, 2023, 1:41am UTC](https://forum.djangoproject.com/t/passing-django-context-data-to-an-external-js-file/3844/6 "2023-08-08T01:41:08Z")

</div>

Learning the wrong ways of doing stuff is the only way of getting things done right, thanks for detailed explanations and multiple ways of accessing the python variables inside js on webpage.

---

<div class="post-metadata">

**Author:** ![groundblue](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/groundblue/32/21232_2.png) [@groundblue](https://forum.djangoproject.com/u/groundblue)\
**Post date:** [May 12, 2024, 12:21am UTC](https://forum.djangoproject.com/t/passing-django-context-data-to-an-external-js-file/3844/7 "2024-05-12T00:21:58Z")

</div>

I think this blog post answers your question : [How to use a Python variable in an external Javascript (Django)](https://techkettle.blogspot.com/2022/03/how-to-use-python-variable-in-external.html)
