【问题标题】:Is there a way to use conditional IF statements in Django Compressor compress tags?有没有办法在 Django Compressor 压缩标签中使用条件 IF 语句?
【发布时间】:2016-02-19 10:33:20
【问题描述】:

有没有办法在使用 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 %}

这是我得到的错误:

请注意,我使用的是 1.4 版的 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

【问题讨论】:

    标签: python django django-compressor


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-30
      • 2020-11-17
      • 2012-01-22
      • 1970-01-01
      • 1970-01-01
      • 2019-12-23
      相关资源
      最近更新 更多