Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (45.3k points)

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

I'm working with Django and python.

Thanks in advance. Regards.

1 Answer

0 votes
by (16.8k points)

django-compressor lets you do this as well as optimize your site by condensing all of your required JS or CSS into one file and optimizing file size.

UPDATE: By default, compressor will convert relative urls into absolute urls using STATIC_URL. If you download the development version, it comes with a django template engine parser which lets you use all django template code directly in your CSS files, such as the {% static %} tag.

https://github.com/jezdez/django_compressor

Enable the TemplateFilter in settings.py after installing to parse your js or css files via the template engine.

COMPRESS_JS_FILTERS = [

    'compressor.filters.template.TemplateFilter',

]

Now any JS within {% compress js %} blocks will be parsed by the django template language...

{% compress js %}

    <script src="/my_script.js" type="text/javascript"></script>

{% endcompress %}

// my_script.js

alert('{{ STATIC_URL|escapejs }}');

You asked for the best way -- I think this is the best. Certainly better than serving dynamic files.

Related questions

0 votes
1 answer
+1 vote
1 answer
0 votes
1 answer
0 votes
1 answer
0 votes
2 answers

Browse Categories

...