Django Compressor compresses static files (js/css)

Source: Internet
Author: User
Tags python mysql django tutorial

Django Compressor compresses static files (js/css)

Static resource files in the website development stage are generally not compressed and merged. This will not only waste bandwidth, but also affect the Website access speed. Django-compressor is used to compress static files into one file during project deployment. The following describes the parameters in the settings configuration file and then discusses how to use Compressor. If you are familiar with the setting file, you can read it directly from the second part.

Part 1: setting Configuration

In earlier versions, django had to process static resources and configure urlpatterns. However, since django1.6django.contrib.staticfilesThis built-in app makes it much easier to process static resources in the development environment.
1.django.contrib.staticfilesIs a built-in django (build-in) app used to process static resources such as js, css, and images. First, make sure that the app is included inINSTALLED_APPSIn, django1.6 is included by default.
2. SpecifySTATIC_URLFor example:

STATIC_URL = '/static/'

STATIC_URL is the root path for the client to access static resources. For example, the resource path defined in the template is:

{% load staticfiles %}<script src="{% static "js/blog.js" %}"></script>

After rendering, the effects are as follows:

<script src="/static/js/blog.js"></script>

3 by default, django searches for static files in the static subdirectory of the app. Therefore, you usually store the static files in their respective app/static directories. Why? Django has a default configuration itemSTATICFILES_FINDERSThe default value is:

    ("django.contrib.staticfiles.finders.FileSystemFinder",    "django.contrib.staticfiles.finders.AppDirectoriesFinder")

From the above we can see that there is a module named AppDirectoriesFinder, which is responsible for finding static files in the app/static directory. We will introduce FileSystemFinder later.
4. common resource files such as jquery and bootstrap are used in multiple different apps. If they are placed in an app, they do not conform to the python philosophy, therefore, django wants to provide a public directory to store these files. A configuration parameter is required:STATICFILES_DIRSFor example:

STATICFILES_DIRS = (    os.path.join(BASE_DIR, "static"),    '/var/www/static/',)

That is to say, static files can be stored in any location on the disk (as long as they have access permissions). Now we should understand the role of FileSystemFinder. Is used to find static files defined in STATICFILES_DIRS.

Deployment

The above is the process of processing static resources in the development environment. How can this problem be solved in the production environment? If this is still the case for django, it will be exhausted. For static resources, it will be handled directly by a proxy such as Nginx.django.contrib.staticfilesA convenient management command is provided to collect static resources under different directories to a unified directory.
1. SetSTATIC_ROOTThis directory is where all static resources are stored.

    STATIC_ROOT="/var/www/foofish.net/static/"

2. Run the collectstatic management command.

    python manage.py collectstatic

This command will copy all STATIC resources to STATICROOT directory.
3. Configure nginx to allow access/static/path requests to directly access STATIC
ROOT.

     location /static {        alias /var/www/foofish.net/static/; # your Django project's static files -       amend as required    }

Summary
Django. contrib. staticfiles is a built-in app for django to process static files. You do not need to configure complex urlpatterns for static resources during development. STATICThe URL is the root path for the client to access static resources. STATICFILESDIRS tells django where static resources are stored, which is parsed by FileSystemFinder. STATICROOT is the place where static resource files in all different places are summarized during project deployment for nginx to use directly. The python manage. py collecstatic command is used to collect resource files from different places to STRTIC.ROOT.

Part 2: compressor

The installation and configuration of django compressor are very simple. The main steps are as follows:

Installation:

pip install django_compressor

Configuration:

COMPRESS_ENABLED = TrueINSTALLED_APPS = (    # other apps    "compressor",)STATICFILES_FINDERS = ('django.contrib.staticfiles.finders.AppDirectoriesFinder','django.contrib.staticfiles.finders.FileSystemFinder','compressor.finders.CompressorFinder',)

DEBUG depends on whether Compress is enabled by default. The default value is COMPRESS.ENABLED is the opposite of DEBUG. Because the compress function is used to compress static files before the project is released in the production environment. Therefore, you need to manually set COMPRESS for testing in the development phase (DEBUG = True ).ENABLED = True

Usage:

{% Load compress %} # process css {% compress css %} <link href = "{% static" css/bootstrap.min.css "%}" rel = "stylesheet"> <link href = "{% static" css/blog-home.css "%}" rel = "stylesheet"> <link href = "{% static" css/github.css "%}" rel = "stylesheet"> {% endcompress %} # handle js {% compress js %} <script src = "{% static" js/jquery-1.10.2.js "%}"> </script> <script src = "{% static" js/bootstrap. js "%}"> </script> <script src = "{% static" js/blog. js "%}"> </script >{% endcompress %}

Run the following command:python manage.py compress, The final file will be merged:

<link rel="stylesheet" href="/static/CACHE/css/f18b10165eed.css" type="text/css"><script type="text/javascript" src="/static/CACHE/js/9d1f64ba50fc.js"></script>

These two files are under the STATIC_ROOT directory.

Django1.8 returns the json string and the content of the json string that receives the post.

How to Use Docker components to develop a Django project?

Install Nginx + uWSGI + Django on Ubuntu Server 12.04

Deployment of Django + Nginx + uWSGI

Django tutorial

Build a Django Python MySQL Linux development environment

Django details: click here
Django's: click here

This article permanently updates the link address:

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.