Describes the static resource manager in the Django framework of Python Django-pipeline_python

Source: Internet
Author: User

Django-pipeline is a very convenient static resource management app, especially after the 1.2 version, using the Django-staticfiles collectstatic command, it is easy to switch in the development and deployment environment.

When writing codinn.com code, static resource management has encountered some annoying things:

    • To speed up the page load, the browser caches the static files, and after the static resources are updated, the browser is likely to fetch stale static files from the cache
    • Write the Css/js to hand minify, very troublesome
    • Fragmented Css/js small files too many
    • After Css/js minified, debugging in the development environment is inconvenient.

Django-pipeline through static resource versioning, automatic minify, group merging CSS/JS files, compatible django-staticfiles URL rules, and collectstatic commands.
Slimit/jsmin

I usually use jsmin/cssmin these two Python minifier to figure out the hassle:

 Pipeline_js_compressor = ' Pipeline.compressors.jsmin.JSMinCompressor '
  pipeline_css_compressor = ' Pipeline.compressors.cssmin.CssminCompressor '

But jsmin years of disrepair, the actual use of the conclusion is: Jsmin is a bug, the JQuery development library with Jsmin Minify was destroyed, does not work properly. But JQuery official minify after the JS file with jsmin minify but no problem.

Although there are circumvention scheme, but always to jsmin not trust, simply switch to Slimit, temporarily haven't found JS damaged problem.

Django-pipeline did not provide support for Slimit, but pipeline's extensibility is good, and 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 above code as a slimit file, throw it in a package directory in your project, and then modify the settings:

Pipeline_js_compressor = ' Lib.slimit.SlimItCompiler '

Unicodedecodeerror: ' ASCII ' codec can ' t decode byte problem

When Django-pipeline is enabled, perform manage.py collectstatic if you encounter the following error message:

 File ".../env/local/lib/python2.7/site-packages/django/contrib/staticfiles/storage.py", line, post_process
    content = pattern.sub (converter, content)
  Unicodedecodeerror: ' ASCII ' codec can ' t decode byte 0xef in position 0:ordinal not in range (128)

Probably because the CSS file contains Chinese or other non-Latin text. I ran into this problem using @font-face icon text, which is a bug in the Django Staticfiles package, and the solution is to modify the/django/contrib/staticfiles/storage.py file, The line of code that went wrong:

Content = Original_file.read ()

Replace with:

 Content = Original_file.read (). Decode (' Utf-8 ')

The premise of this method is that your CSS file must be utf-8 encoded, or it will still be wrong.

PS, submitted a bug report to the Django project: https://code.djangoproject.com/ticket/18430

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.