2015-11-17 58 views
2

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

我试过了,但它产生了一个错误。下面是我所累的例子:

{% 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压缩机的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 

回答

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 %} 
相关问题