Search code examples
pythondjangodjango-templatesminifydjango-template-filters

Is there a way to include multiple javascript files in a django template?


Write now in my Django template I have something that looks like this with like 10 files:

<script type="text/javascript" src="{% static "/js/main_1.js" %}"></script> <script type="text/javascript" src="{% static "/js/main_2.js" %}"></script> <script type="text/javascript" src="{% static "/js/main_3.js" %}"></script>

What I was wondering was if there was a way to do:

<script type="text/javascript" src="{% static ["/js/main_1.js","/js/main_2.js","/js/main_3.js"] %}"></script>

and then have python have a minified version of all them.


Solution

  • There is not a great way to combine when you are linking the scripts in your template. You can write a script that will generate URLs for you, but unless the number is unmanageable I wouldn't recommend that. 10 is not so many.

    For the minifying I would use Django Compressor. It will take your linked javascript and turn them into minified, cacheable files automatically.