I'm using django-compressor to compress my site's static CSS and Javascript files. Since I serve my site's static assets via Amazon S3, I'm also using django-storages to upload my files to S3.
Here's my issue: I'm trying to make a clean base.html
template that all my site's other templates can inherit and extend. Here's what it looks like currently:
{% load compress %}
<html>
<head>
<!-- test -->
{% compress css %}
<link rel="stylesheet" type="text/css" media="screen" href="{{ STATIC_URL }}css/styles.css" />
{% endcompress %}
{% compress css %}
{% block css %}{% endblock %}
{% endcompress %}
{% compress js %}
{% block js %}{% endblock %}
{% endcompress %}
</head>
<body>
{% block body %}{% endblock %}
</body>
</html>
As you can see, what I'm attempting to do here is allow my templates that inherit this template to override the css
and js
blocks, so they can define their own css and javascript to be compressed. Unfortunately, that's not what happens.
When I run python manage.py compress
(to have django-compressor analyze my templates and generated the compressed javascript and css code), it doesn't actually find my included css and javascript files.
For instance, here's my site's index.html
template:
{% block css %}
{{ block.super }}
<link rel="stylesheet" type="text/css" media="screen" href="{{ STATIC_URL }}css/index.css" />
{% endblock %}
When I attempt to visit that page on my site, I get an error saying that the compressed file doesn't exist.
I believe that what's happening is that the python manage.py compress
command isn't inspecting my templates that inherit from base.html
. And since it isn't analyzing them, it isn't generating any compressed code.
I'd really like to get this working, because the only workaround I've found so far is to manually add {% compress %}...{% endcompress %}
tags in every single template file I have explicitly. I just hate to do that since it repeats so much code everywhere :(
Any advice would be greatly appreciated.
The main use of Django Compressor is to compress linked and inline JavaScript and CSS into a single cached file.
Template inheritance. The most powerful – and thus the most complex – part of Django's template engine is template inheritance. Template inheritance allows you to build a base “skeleton” template that contains all the common elements of your site and defines blocks that child templates can override.
You will work with django-compressor to compress CSS and JS and django-htmlmin to minify HTML files. You can refer to the source code of this project here. Django Compressor is a python package for the Django web framework to compress and minify CSS and JS files using {% load compress %} tag.
I suppose you are using offline compression, in which case the template inheritance does not work as one would expect. See these issues relevant to this "problem":
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With