A brief analysis of STATIC_ROOT, STATIC_URL and STATICFILES_DIRS in Django

不言
Release: 2018-05-08 11:49:07
Original
1565 people have browsed it

This article mainly introduces you to the relevant information about STATIC_ROOT, STATIC_URL and STATICFILES_DIRS in Django. The article introduces it in detail through sample code. It has certain reference learning value for everyone's study or work. Friends who need it can follow Let’s take a look together

Preface

This article mainly introduces to you the relevant content about STATIC_ROOT, STATIC_URL and STATICFILES_DIRS in Django. Share it for your reference. Please refer to it for reference and study. I won’t say much more below. Let’s take a look at the detailed introduction.

The details are as follows:

First, we configure the static file and add the following lines of code to setting.py:

settings.py

# the settings above
# STATIC SETTINGS
STATIC_URL = '/static/'
# BASE_DIR 是项目的绝对地址
STATIC_ROOT = os.path.join(BASE_DIR, 'collect_static')
#以下不是必须的
STATICFILES_DIRS = (
 os.path.join(BASE_DIR, 'common_static'),
)
Copy after login

1.STATIC_ROOT

STATIC_ROOT is when deploying static files ( pyhtonmanage.pycollectstatic) For all static static aggregation directories, STATIC_ROOT must be written as an absolute address. Here, for example, my project mysite is /home/mysite/

Then STATIC_ROOT is /home/mysite/collect_static /

When deploying the project, enter in the terminal:

python manage.py collectstatic
Copy after login

django will copy all static files to the STATIC_ROOT file Folder

2.STATICFILES_DIRS

STATIC_ROOT only comes into play during deployment. In actual situations, there are two general placement locations for static files:

1. One is to create a new static folder in each app and put the static files in it. When loading the static files, for example, if you want to use static files in the template, Django will automatically add them to each app. Search the static folder inside (so, don’t write the name of the folder wrong, otherwise django will not be able to find your folder)

2. The other way is to search outside all app files. Create a public folder. Because some static files are not unique to a certain app, you can put them in a public folder for easy management (note that creating a public static file folder is just an easy way to Management method, but it is not necessary. Apps can apply static files across apps, because in the end all static files will exist in STATIC_ROOT)

The question now is how to let django know you To put some static files in a public folder outside of the app, you need to configure STATICFILES_DIRS

STATICFILES_DIRS = (
 os.path.join(BASE_DIR, 'common_static'),
)
Copy after login

STATICFILES_DIRS tells django to first look for static files in STATICFILES_DIRS files, and then search in the static folder of each app (note that django searches for static files in a lazy manner, and stops searching when it finds the first one)

3.STATIC_URL

So far, the static file mechanism can work, but there is a question, can I directly access the static files in my project through the URL? The answer is definitely yes, but please note, You are accessing it through a browser. You cannot enter the local absolute address of your static file. For example, the local address of one of my pictures is /home/mysite/common_static/myapp/photo.png.


Then it is impossible for others to enter directly on the browser:


http://192.168.1.2:8000/home/mysite/common_static/myapp/photo.png


In this case, the browser will report an error, there is no such page


So how does Django allow the browser to access static files on the server, as I have already said , it is not possible to directly access the local address of the server, then a mapping is needed. Django uses STATIC_URL to allow the browser to directly access static files, such as:

STATIC_URL = '/static/'
Copy after login

Then you can enter on the browser:


http://192.168.1.2:8000/static/common_static/myapp/photo.png


Then it is equivalent to Visit /home/mysite/common_static/myap/photo.png

So on the browser, use the specific content of the prefix STATIC_URL to map STATIC_ROOT,

HTTP://192.168.1.2:8000/static Equivalent to STATIC_ROOT of the local address


Related recommendations:

How to use Ajax in Django

How to prevent the form page in Django from refreshing Automatic submission

The above is the detailed content of A brief analysis of STATIC_ROOT, STATIC_URL and STATICFILES_DIRS in Django. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!