有没有办法在Django Compressor压缩标签中使用条件IF语句?

时间:2015-11-17 21:30:07

标签: python django django-compressor

有没有办法在使用Django Compressor的Django模板的{% compress %}标签内使用条件语句?

我已经尝试过,但它会产生错误。这是我累了的一个例子:

{% 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 %}

这是我得到的错误:

请注意我使用的是Django Compressor的1.4版本。

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

1 个答案:

答案 0 :(得分:3)

usage examples in the documentation中没有一个包含条件,而review of the code for the compress template tag则表明这是因为它可能无法实现。

相反,请考虑使用两个单独的compress块:

{% 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 %}