This article mainly introduces the configuration of static files in Djang. django static file configuration is mainly used to allow users to find the static file returned when requesting the django server, for more information about how to configure static files in Djang, see the following illustration.
I. django static file configuration principle
Static file configuration is used to allow the django server to find the static file to return when the user requests.
First, we need to understand several 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 item
In the django template, you can reference the {STATIC_URL} variable to avoid writing the path to death. The default settings are usually used.
The code is as follows:
STATIC_URL = '/static /'
ADMIN_MEDIA_PREFIX configuration item
ADMIN_MEDIA_PREFIX must be configured as follows so that staticfiles can correctly find the static resources of django. contrib. admin:
The code is as follows:
ADMIN_MEDIA_PREFIX = STATIC_URL + 'admin /'
Use STATIC_URL in the template
The code is as follows:
After understanding the principles, we will introduce the specific configurations of static files in the development and production environments.
My django version is 1.4.21.
The code is as follows:
>>> Import django
>>> Print django. get_version ()
1.4.21
2. development environment
By default, the staticfiles app is installed in django1.4 and 21. Therefore, you do not need to configure django to access static files in the development environment.
One thing: the sequence of searching static files in the development environment staticfiles depends on the STATIC_FINDERS configuration item, which is configured by default.
The code is as follows:
STATICFILES_FINDERS = (
'Django. contrib. staticfiles. finders. FileSystemFinder ',
'Django. contrib. staticfiles. finders. AppDirectoriesFinder ',
# 'Django. contrib. staticfiles. finders. ultultstoragefinder ',
)
FileStstemFinder is used to search for additional static files in the path specified by STATICFILES_DIRS [null by default. Common resource files such as jquery and bootstrap are used in multiple different apps. django provides a public directory to store these files. the configuration parameter is STATICFILES_DIRS.
AppDirectoriesFinder searches for resource files from the static Directory of the APP package in the INSTALLED_APPS tuples.
Use the following code.
1. create a 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
Create a static directory under hello app to store static files.
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 static hello.txt file in the staticdirectory.
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 hello app in INSTALLED_APPS in the settings. py file of the project.
The code is as follows:
INSTALLED_APPS = (
...
'Hello ',
)
4. run the project
The code is as follows:
[Root @ yl-web lxyproject] # python manage. py runserver 0.0.0.0: 9000
5. access through url
Create an imagesdirectory under the staticdirectory and put a sheep.png Image. Similarly, access the image through a url.
III. production environment
As mentioned above, in the generation environment, it is helpful to use Lighttpd/Nginx to host static resources by storing static resources in a centralized manner. In the production environment, static files are usually stored in the static directory under the root directory of the project.
Default configuration
The code is as follows:
STATIC_ROOT =''
STATIC_URL = '/static /'
STATICFILES_DIRS = (
)
What we need to do is
1. configure the server. I use apache.
For installation configuration details, refer:
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 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/sheep.png'
1 static file copied.
5. configure the server and direct the/static/path to the STATIC_ROOT directory.
Configure in the VM
The code is as follows:
Alias/static // srv/lxyproject/collectedstatic/
6. the configuration is now complete and accessed through a browser
When a django project is actually released, additional work may be required to manage static files:
Less/CoffeeScript and other automatic compilation into css/js
"Compress" css/js files to speed up browser loading
Merge fragmented css/js files to reduce the number of browser requests
Static resource file version: The browser caches static files. after the background code and static resource files are updated, the browser may extract expired static resources from the cache, resulting in abnormal page display.
The above is all about the static file configuration method in Djang. I hope you will like it.