Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Detailed introduction of STATIC_ROOT, STATIC_URL and STATICFILES_DIRS in Django

2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article introduces the relevant knowledge of "detailed introduction of STATIC_ROOT and STATIC_URL and STATICFILES_DIRS in Django". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Details are as follows:

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

Settings.py

# the settings above# STATIC SETTINGSSTATIC_URL ='/ static/'# BASE_DIR is the absolute address of the project STATIC_ROOT = os.path.join (BASE_DIR, 'collect_static') # below is not required STATICFILES_DIRS = (os.path.join (BASE_DIR,' common_static'),)

1.STATIC_ROOT

STATIC_ROOT is the directory where all static files are aggregated when deploying static files (pyhtonmanage.pycollectstatic). The STATIC_ROOT should 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 at the terminal:

Python manage.py collectstatic

Django will copy all the static files to the STATIC_ROOT folder

2.STATICFILES_DIRS

STATIC_ROOT only works at deployment time, but in reality, there are two general locations for static files:

1. One is to create a new static folder in each app and put static files in it. When loading static files, such as using static files in templates, django will automatically search for static folders in each app (so don't write the name of the folder wrong, or django won't find your folder).

two。 Another way is to set up a public folder outside all app files, because some static files are not unique to an app, so you can put them in a public folder for easy management. (note that creating a folder of public static files is easy to manage, but not necessary. App can apply static files across app. Because in the end, all static files will exist in STATIC_ROOT)

So the question now is how to let django know that you put some static files in a public folder other than app, so you need to configure STATICFILES_DIRS.

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

STATICFILES_DIRS told django to look for static files in STATICFILES_DIRS first, and then in the static folder of each app. (note that django looking for static files is a lazy lookup. When you find the first one, you stop looking.)

3.STATIC_URL

So at this point, the static file mechanism can work, but there is a question, can I directly access my static files in the project through url? the answer must be yes, but, note, you are visiting in the browser, you can't enter the local absolute address of your static file, such as the local address of one of my pictures.

For / 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 way, the browser will report an error and there is no page.

So how does django enable browsers to access static files on the server? as mentioned earlier, it is not possible to access the local address of the server directly, so you need a mapping. Django uses STATIC_URL to enable browsers to access static files directly, such as:

STATIC_URL ='/ static/'

Then you can enter on the browser:

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

Then it is equivalent to visiting / 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 is equivalent to STATIC_ROOT of local address

This is the end of the detailed introduction of STATIC_ROOT, STATIC_URL and STATICFILES_DIRS in Django. Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report