Skip to main content
 首页 » 编程设计

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

2024年08月23日34wayfarer

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

请您参考如下方法:

没有usage examples in the documentation包括条件,和一个 review of the code for the compress template tag建议那是因为它可能是不可能的。

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

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