2

I am trying to use STATIC_URL in external javascript file. I was expecting the result as it is working in template, but I found that it is not working in external Javascript file.

Please also tell me some work around to make STATIC_URL work in javascript file as it will make my project more manageable.

Also I am sending many ajax request and i want to have something like url template tag in my JS file.

Please let me know if you know solution of any of them.

Thanks

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

There is no other go for this other than passing it through template
1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.