Using django-compressor with render_block from sekizai_tags

700 views Asked by At

I would like to be able to

  1. Compile all js/css into single minified files, which is what django-compressor does.
  2. Be able to add js/css files from included and inherited templates.

When I try:

{% compress css file %}
{% render_block "css" %}
{% endcompress %}

I get the error: Invalid block tag: 'endcompress'

But I think the main cause is from the sekizai docs:

{% render_block %} tags must not be placed inside a template tag block (a template tag which has an end tag, such as {% block %}...{% endblock %} or {% if %}...{% endif %}).

Have I done something wrong or is there another way?

1

There are 1 answers

0
jozxyqk On

It seems django-compressor explicitly provides this interaction, but not by putting render_block inside compress/endcompress:

{% load sekizai_tags %}
{% render_block "<js/css>" postprocessor "compressor.contrib.sekizai.compress" %}

In fact, the {% compress %} tag is simply not used here.

<head>
...
<!-- css -->
{% render_block "css" postprocessor "compressor.contrib.sekizai.compress" %}
{% block css %}{% endblock %}
<!-- end js -->
...
</head>

... then in another template ...

{% block css %}
{% addtoblock "css" %}
    {{ form.media.css }}
    <link rel="stylesheet" type="text/css" href="..." />
{% endaddtoblock %}
{% endblock %}

I use block css just for the sake of having any block to put addtoblock in, otherwise it won't work (unless it's in the same template file):

If the {% addtoblock %} tag is used in an extending template, the tags must be placed within {% block %}...{% endblock %} tags.