shrewdbeans
shrewdbeans

Reputation: 12539

Is there a way to use conditional IF statements in Django Compressor compress tags?

Is there a way to use a conditional statement from within the {% compress %} tags in a Django template that uses the Django Compressor?

I've tried it but it produces an error. Here's an example of what I've tired:

{% compress js %}
    <script src="{{ STATIC_URL }}js/example-1.js"></script>
    <script src="{{ STATIC_URL }}js/example-2.js"></script>
    {% if settings.EXAMPLE %}
    <script src="{{ STATIC_URL }}js/example-3.js"></script>
    {% endif %}
{% endcompress %}

And this is the error I get:

Please note I'm using version 1.4 of Django Compressor.

Validating models...

0 errors found
Django version 1.4.12, using settings 'ukcms.settings'
Development server is running at http://0.0.0.0:8014/
Quit the server with CONTROL-C.
----------------------------------------
Exception happened during processing of request from ('127.0.0.1', 64989)
----------------------------------------
[17/Nov/2015 21:25:28] "GET / HTTP/1.1" 200 87387
Traceback (most recent call last):
  File "/usr/local/Cellar/python/2.7.8_1/Frameworks/Python.framework/Versions/2.7/lib/python2.7/wsgiref/handlers.py", line 86, in run
    self.finish_response()
  File "/usr/local/Cellar/python/2.7.8_1/Frameworks/Python.framework/Versions/2.7/lib/python2.7/wsgiref/handlers.py", line 128, in finish_response
    self.write(data)
  File "/usr/local/Cellar/python/2.7.8_1/Frameworks/Python.framework/Versions/2.7/lib/python2.7/wsgiref/handlers.py", line 217, in write
    self._write(data)
  File "/usr/local/Cellar/python/2.7.8_1/Frameworks/Python.framework/Versions/2.7/lib/python2.7/socket.py", line 324, in write
    self.flush()
  File "/usr/local/Cellar/python/2.7.8_1/Frameworks/Python.framework/Versions/2.7/lib/python2.7/socket.py", line 303, in flush
    self._sock.sendall(view[write_offset:write_offset+buffer_size])
error: [Errno 41] Protocol wrong type for socket
[17/Nov/2015 21:25:28] "GET / HTTP/1.1" 500 59
Traceback (most recent call last):
  File "/usr/local/Cellar/python/2.7.8_1/Frameworks/Python.framework/Versions/2.7/lib/python2.7/SocketServer.py", line 595, in process_request_thread
    self.finish_request(request, client_address)
  File "/usr/local/Cellar/python/2.7.8_1/Frameworks/Python.framework/Versions/2.7/lib/python2.7/SocketServer.py", line 334, in finish_request
    self.RequestHandlerClass(request, client_address, self)
  File "/Users/owen/src/uktv/ukcms/lib/python2.7/site-packages/django/core/servers/basehttp.py", line 139, in __init__
    super(WSGIRequestHandler, self).__init__(*args, **kwargs)
  File "/usr/local/Cellar/python/2.7.8_1/Frameworks/Python.framework/Versions/2.7/lib/python2.7/SocketServer.py", line 653, in __init__
    self.finish()
  File "/usr/local/Cellar/python/2.7.8_1/Frameworks/Python.framework/Versions/2.7/lib/python2.7/SocketServer.py", line 712, in finish
    self.wfile.close()
  File "/usr/local/Cellar/python/2.7.8_1/Frameworks/Python.framework/Versions/2.7/lib/python2.7/socket.py", line 279, in close
    self.flush()
  File "/usr/local/Cellar/python/2.7.8_1/Frameworks/Python.framework/Versions/2.7/lib/python2.7/socket.py", line 303, in flush
    self._sock.sendall(view[write_offset:write_offset+buffer_size])
error: [Errno 32] Broken pipe

Upvotes: 2

Views: 524

Answers (1)

user764357
user764357

Reputation:

None of the usage examples in the documentation include conditionals, and a review of the code for the compress template tag suggests thats because its probably not possible.

Instead, consider using two separate compress blocks:

{% compress js %}
    <script src="{{ STATIC_URL }}js/example-1.js"></script>
    <script src="{{ STATIC_URL }}js/example-2.js"></script>
{% endcompress %}
{% if settings.EXAMPLE %}
    {% compress js %}
        <script src="{{ STATIC_URL }}js/example-3.js"></script>
    {% endcompress %}
{% endif %}

Upvotes: 3

Related Questions