The following through an illustrated method to give you a detailed introduction to the static file configuration method in Djang
First, Django static file configuration principle
The static file configuration is intended to allow the Django server to find a static file return when requested by the user.
The first thing to understand is a few concepts:
The code is as follows:
Site_root = Os.path.dirname (Os.path.abspath (__file__))
Site_root = Os.path.abspath (Os.path.join (Site_root, ' ... /'))
Static_root = Os.path.join (site_root, ' collectedstatic ')
static_url Configuration Items
In a Django template, you can refer to the {{Static_url}} variable to avoid writing the path to death. Default settings are typically used
The code is as follows:
Static_url = '/static/'
admin_media_prefix Configuration Items
The Admin_media_prefix must be configured as follows so that staticfiles can correctly locate django.contrib.admin static resources:
The code is as follows:
Admin_media_prefix = Static_url + ' admin/'
Using Static_url in Templates
The code is as follows:
After the principle is understood, the next step is to introduce the configuration of static files in the development environment and production environment.
My Django version is 1.4.21, which is described in this release.
The code is as follows:
>>> Import Django
>>> Print django.get_version ()
1.4.21
Second, the development environment
The Staticfiles app is already installed by default in django1.4,21, so access to static files in the development environment does not require any configuration of Django.
One thing: The development environment staticfiles the order in which static files are found depends on the Static_finders configuration item, the default configuration
The code is as follows:
Staticfiles_finders = (
' Django.contrib.staticfiles.finders.FileSystemFinder ',
' Django.contrib.staticfiles.finders.AppDirectoriesFinder ',
# ' Django.contrib.staticfiles.finders.DefaultStorageFinder ',
)
Fileststemfinder is used to find additional static files in the path specified by the staticfiles_dirs "default to NULL". Common resource files such as Jquery,bootstrap are used in a number of different apps, and Django provides a public directory to put these files, which is the configuration parameter: Staticfiles_dirs
Appdirectoriesfinder looks for a resource file from the static directory of the package in which the app in the Installed_apps tuple resides.
Use the following.
1. New Project Lxyproject
The code is as follows:
[Root@yl-web srv]# django-admin.py startproject lxyproject
2. Create an app named Hello in the project
The code is as follows:
[Root@yl-web lxyproject]# python manage.py startapp Hello
[Root@yl-web lxyproject]# ls
Hello Lxyproject manage.py
A static directory is built under the Hello app to hold the files at rest.
The code is as follows:
[Root@yl-web hello]# mkdir Static
[Root@yl-web hello]# ls
__init__.py models.py Static tests.py views.py
Then create a new hello.txt static file in the static directory.
The code is as follows:
[Root@yl-web hello]# CD static/
[Root@yl-web static]# ls
Hello.txt
[Root@yl-web static]# Cat Hello.txt
Hello.txt ' s content:congratulations!!
3. Configure the Hello app in the settings.py file of the project Installed_apps
The code is as follows:
Installed_apps = (
...
' Hello ',
)
4. Running the project
The code is as follows:
[Root@yl-web lxyproject]# python manage.py runserver 0.0.0.0:9000
5. Access via URL
Create a new images directory under the static directory and put a picture of sheep.png in the same URL.
Third, the production environment
As I said earlier, in a build environment, centralizing static resources facilitates the use of Lighttpd/nginx to host static resources. The production environment typically places static files under the static directory in the project root directory.
Default configuration
The code is as follows:
Static_root = ' '
Static_url = '/static/'
Staticfiles_dirs = (
)
What we're going to do is
1, configure the server, I use Apache.
Installation configuration details can be found in:
The code is as follows:
[Root@yl-web lxyproject]# python manage.py collectstatic
You have requested to collect static files at the destination
Location as specified in your settings.
This would overwrite existing files!
Is you sure your want to does this?
Type ' yes ' to continue, or ' no ' to Cancel:yes
Copying '/srv/lxyproject/hello/static/images/sheep.png '
1 static file copied.
5. Configure the server and point the/static/path to the Static_root directory.
That is, configure in the virtual host
The code is as follows:
alias/static//srv/lxyproject/collectedstatic/
6, configuration to complete, through the browser access
When you actually publish a Django project, the management of the static file may require additional work:
Less/coffeescript, etc. automatically compiled into CSS/JS
"Compress" CSS/JS files to improve browser loading speed
Reduce browser requests by merging fragmented CSS/JS files
The version of the static resource file: The browser caches the static files, the background code and the static resource files are updated, and the browser is likely to extract outdated static resources from the cache, causing the page to show an exception.
The above is the static file configuration method in Djang all content, I hope you like.