SoFunction
Updated on 2024-11-21

Static file configuration methods in Djang

The following graphic method to give you a detailed introduction to the static file configuration method in Djang

First, django static file configuration principle
Static files are configured so that the django server can find static files to return when a user requests them.

First of all, it is important to understand a few concepts:

Media files: files uploaded by users
Static files: css, js, image, etc.
Development environment: using django built-in server to handle static files
Production environment: using apache2/nginx server to handle static file mapping
So when you configure it, you have to distinguish between the development environment and the production environment, which will be described in detail later.

Here's a quick overview of how the server looks for static files so we can configure it better.

1、
is django1.3 added an app to help developers manage static files [js, css, etc.].

Static files before django 1.3 were controlled with MEDIA_URL and MEDIA_ROOT.

in order to incorporatemedia file[Files uploaded by users] andStatic filesTo make the distinction, django 1.3 handles media files via the MEDIA_XXX configuration and static files via the STATIC_XXX configuration item.

staticfiles enables developers to assign static files to the app directory or any specified directory.

2、Difference between MEDIA_XXX and STATIC_XXX configuration items
The MEDIA_XXX configuration item is used to manage media files. Often uploaded by the FileFields field, they are stored in the directory specified by settings.MEDIA_ROOT and accessed via the path specified by settings.MEDIA_URL.

The STATIC_XXX configuration item is used to manage static files. They are assembled into the settings.STATIC_ROOT directory via the collectstatic command and accessed via the path specified by settings.STATIC_URL.

3, static file-related configuration items in detail
STATIC_ROOT configuration item

Used to specify the path where static files are stored when collectstatic is executed. In a generative environment, centralized storage of static resources facilitates the use of Lighttpd/Nginx/apache2 to host static resources. For debugging purposes, it is usually set as follows:

Copy Code The code is as follows.

SITE_ROOT = ((__file__))
SITE_ROOT = ((SITE_ROOT, '../'))
STATIC_ROOT = (SITE_ROOT, 'collectedstatic')

STATIC_URL configuration item

In django templates, you can reference the {{STATIC_URL}} variable to avoid writing the path to death. It is common to use the default setting

Copy Code The code is as follows.

STATIC_URL = '/static/'

ADMIN_MEDIA_PREFIX configuration item

ADMIN_MEDIA_PREFIX must be configured as follows for static resources to be found correctly by staticfiles:

Copy Code The code is as follows.

ADMIN_MEDIA_PREFIX = STATIC_URL + 'admin/'

Using STATIC_URL in templates

Copy Code The code is as follows.

<link rel="stylesheet" href="{{ STATIC_URL }}css/">

After the principle of understanding, the next introduction to the development environment and production environment in the static file specific configuration.

My django version is 1.4.21, so I'll use this version as an introduction.

Copy Code The code is as follows.

>>> import django
>>> print django.get_version()
1.4.21

II. Development environment
The staticfiles app is installed by default in django 1.4,21, so there is no need to configure django for static file access in the development environment.

One thing: the order in which the development environment staticfiles looks for static files depends on the STATIC_FINDERS configuration item, the default configuration is

Copy Code The code is as follows.

STATICFILES_FINDERS = (
    '',
    '',
#    '',
)

FileStstemFinder is used to find additional static files in the path specified with STATICFILES_DIRS [empty by default]. Common resource files such as jquery, bootstrap, etc. are shared among several different apps. django provides a public directory for these files, and this configuration parameter is STATICFILES_DIRS.
AppDirectoriesFinder looks for resource files from the static directory of the package where the APP is located in the INSTALLED_APPS tuple.
Use the following.

1、New project lxyproject

Copy Code The code is as follows.

[root@yl-web srv]# startproject lxyproject

2, in the project create a new app named hello

Copy Code The code is as follows.

[root@yl-web lxyproject]# python startapp hello
[root@yl-web lxyproject]# ls
hello  lxyproject 

Create a static directory under hello app for static files.

Copy Code The code is as follows.

[root@yl-web hello]# mkdir static
[root@yl-web hello]# ls
__init__.py    static   

Then create a new static file in the static directory.

Copy Code The code is as follows.

[root@yl-web hello]# cd static/
[root@yl-web static]# ls

[root@yl-web static]# cat
's content:congratulations!!

3. Configure the hello app in the project file INSTALLED_APPS.

Copy Code The code is as follows.

INSTALLED_APPS = (
    ...
    'hello',
)

4. Running the project

Copy Code The code is as follows.

[root@yl-web lxyproject]# python runserver 0.0.0.0:9000

5. Access via url

In the static directory, create a new images directory to put a picture, the same way through the url to access the

III. Production environment
As mentioned earlier, in a generation environment, centralizing static resources is beneficial to use Lighttpd/Nginx to host static resources. And the production environment usually puts the static files in the static directory under the root directory of the project.

default configuration

Copy Code The code is as follows.

STATIC_ROOT = ''
STATIC_URL = '/static/'
STATICFILES_DIRS = (
)

Here's what we're going to do.

1, configure the server, I use apache.

Installation and configuration details are available:

Ubuntu apache2 server configuration

centos7 apache httpd install and configure django project

2. Create the collectedstatic directory in the project root directory.

3. Configuration

import os

SITE_ROOT = ((__file__))
SITE_ROOT = ((SITE_ROOT, '../'))
STATIC_ROOT = (SITE_ROOT, 'collectedstatic')
STATIC_URL = '/static/'
STATICFILES_DIRS = (
 # Put strings here, like "/home/html/static" or "C:/www/django/static".
 # Always use forward slashes, even on Windows.
 # Don't forget to use absolute paths, not relative paths.
)
INSTALLED_APPS = (
 '',
 '',
 '',
 '',
 '',
 '',
 # Uncomment the next line to enable the admin:
 # '',
 # Uncomment the next line to enable admin documentation:
 # '',
 'hello',
)

 
4, run python collectstatic to collect static files of all installed APPs to the root directory collectedstatic [i.e. STATIC_ROOT].

 

Copy Code The code is as follows.

[root@yl-web lxyproject]# python collectstatic
You have requested to collect static files at the destination
location as specified in your settings.
This will overwrite existing files!
Are you sure you want to do this?
Type 'yes' to continue, or 'no' to cancel: yes
Copying '/srv/lxyproject/hello/static/images/'
1 static file copied.
 
5. Configure the server to point the /static/ path to the STATIC_ROOT directory.

That is, configure the

Copy Code The code is as follows.

Alias /static/ /srv/lxyproject/collectedstatic/

6、Configuration is complete, access through the browser

 


When actually releasing a django project, the management of static files may require some additional work:

Less / CoffeeScript etc. auto-compile to css/js
"Compress" css/js files to improve browser loading speeds
Consolidate fragmented css/js files to reduce browser requests
Versioning of static resource files: Browsers cache static files. When both the background code and static resource files are updated, the browser is likely to fetch outdated static resources from the cache, resulting in abnormal page display.

This is all about the static file configuration method in Djang, hope you like it.