How to make STATIC_URL work in external JS Files (Django)

孤人 提交于 2019-12-06 07:57:37

I use a simple Django app for making Python / Django settings available in javascript.

https://github.com/incuna/django-settingsjs

It works in the same way by rendering the settings in a template but provides a mechanism for making multiple variables available in JavaScript using a view, urls and optionally signals.

My solution to this problem is to pass STATIC_URL to your JavaScript code at some point so that it also knows the value. Something like this:

<script type="text/javascript">
myJavaScript('{{ STATIC_URL }}');
</script>

Your JavaScript function myJavaScript can then use it in URLs and wherever it is needed.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!