Alex Rivera | Logout

Passing STATIC_URL to file javascript with django

Asked 2012-03-06T01:33:51.160
21

Which is the best solution to pass {{ STATIC_URL }} to javascript files?

I'm working with django and python.

Thanks in advance. Regards.

Edit
Report

1 Answer

38

Using a global javascript variable with the static url value is more simple :

<script language="javascript">var STATIC_URL = "{{ STATIC_URL|escapejs }}";</script>
<script src="{{ STATIC_URL }}js/myfile.js"></script>

Then, you can simply use the static url by calling STATIC_URL in myfile.js :

html = '<img src="'+STATIC_URL+'/icons/flags/tn.gif">';
answered 2012-10-14T11:12:38.610

Your Answer