This section describes the static resource manager Django-pipeline and pythondjango in Python's django framework.
Django-pipeline is a very convenient static resource management app in Django. Especially after version 1.2, It is very convenient to use the collectstatic command of django-staticfiles in the development and deployment environments.
When writing codinn.com Code, some annoying things were encountered in static resource management:
- To speed up page loading, the browser caches static files. After static resources are updated, the browser may extract expired static files from the cache.
- Manual minify is required for self-written CSS/JS, which is very troublesome.
- There are too many small CSS/JS files.
- After CSS/JS minified, debugging in the development environment is inconvenient.
Django-pipeline uses static resource version, automatic minify, CSS/JS files grouped and merged, and is compatible with django-staticfiles URL rules and collectstatic commands.
Slimit/jsmin
The figure is easy to understand. I usually use the jsmin/cssmin Python minifier:
PIPELINE_JS_COMPRESSOR = 'pipeline.compressors.jsmin.JSMinCompressor' PIPELINE_CSS_COMPRESSOR = 'pipeline.compressors.cssmin.CssminCompressor'
However, jsmin has been out of service for a long time. The actual use conclusion is that jsmin has a bug. The jQuery Development Library is damaged after jsmin minify, so it cannot work properly at all. However, JS files after jQuery's official minify use jsmin minify.
Although there are preventive solutions, we are always uneasy about jsmin and simply switch to slimit. We haven't found any JS damages yet.
Django-pipeline does not support slimit, but pipeline has good scalability. Writing a slimit extension is also a matter of minutes:
from __future__ import absolute_import from pipeline.compilers import CompilerBase class SlimItCompiler(CompilerBase): """ JS compressor based on the Python library slimit (http://pypi.python.org/pypi/slimit/). """ def compress_js(self, js): from slimit import minify return minify(js)
Save the preceding code as a slimit file and place it in a package directory of the project. Then, modify settings:
PIPELINE_JS_COMPRESSOR = 'lib.slimit.SlimItCompiler'
UnicodeDecodeError: 'ascii 'codec can't decode byte Problem
After django-pipeline is enabled, run manage. py collectstatic if the following error message is displayed:
File ".../ENV/local/lib/python2.7/site-packages/django/contrib/staticfiles/storage.py", line 226, in post_process content = pattern.sub(converter, content) UnicodeDecodeError: 'ascii' codec can't decode byte 0xef in position 0: ordinal not in range(128)
The reason is that the CSS file contains Chinese or other non-Latin characters. I encountered this problem when using the @ font-face icon text. This is a bug in the django staticfiles package. The solution is to modify/django/contrib/staticfiles/storage. in The py file, run the following code:
content = original_file.read()
Replace:
content = original_file.read().decode('utf-8')
The premise for using this method is that your CSS file must be UTF-8 encoded; otherwise, errors will still occur.
PS, submitted a bug report to the Django project: https://code.djangoproject.com/ticket/18430