


A brief analysis of STATIC_ROOT, STATIC_URL and STATICFILES_DIRS in Django
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'), )
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
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'), )
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/'
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:
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











Steps to check the Django version: 1. Open a terminal or command prompt window; 2. Make sure Django has been installed. If Django is not installed, you can use the package management tool to install it and enter the pip install django command; 3. After the installation is complete , you can use python -m django --version to check the Django version.

Django and Flask are both leaders in Python Web frameworks, and they both have their own advantages and applicable scenarios. This article will conduct a comparative analysis of these two frameworks and provide specific code examples. Development Introduction Django is a full-featured Web framework, its main purpose is to quickly develop complex Web applications. Django provides many built-in functions, such as ORM (Object Relational Mapping), forms, authentication, management backend, etc. These features allow Django to handle large

Django is a complete development framework that covers all aspects of the web development life cycle. Currently, this framework is one of the most popular web frameworks worldwide. If you plan to use Django to build your own web applications, then you need to understand the advantages and disadvantages of the Django framework. Here's everything you need to know, including specific code examples. Django advantages: 1. Rapid development-Djang can quickly develop web applications. It provides a rich library and internal

How to upgrade Django version: steps and considerations, specific code examples required Introduction: Django is a powerful Python Web framework that is continuously updated and upgraded to provide better performance and more features. However, for developers using older versions of Django, upgrading Django may face some challenges. This article will introduce the steps and precautions on how to upgrade the Django version, and provide specific code examples. 1. Back up project files before upgrading Djan

The differences are: 1. Django 1.x series: This is an early version of Django, including versions 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 1.7, 1.8 and 1.9. These versions mainly provide basic web development functions; 2. Django 2.x series: This is the mid-term version of Django, including 2.0, 2.1, 2.2 and other versions; 3. Django 3.x series: This is the latest version series of Django. Including versions 3.0, 3, etc.

django is the backend. Details: Although Django is primarily a backend framework, it is closely related to front-end development. Through features such as Django's template engine, static file management, and RESTful API, front-end developers can collaborate with back-end developers to build powerful, scalable web applications.

How to check the django version: 1. To check through the command line, enter the "python -m django --version" command in the terminal or command line window; 2. To check in the Python interactive environment, enter "import django print(django. get_version())" code; 3. Check the settings file of the Django project and find a list named INSTALLED_APPS, which contains installed application information.

Django is a web application framework written in Python that emphasizes rapid development and clean methods. Although Django is a web framework, to answer the question whether Django is a front-end or a back-end, you need to have a deep understanding of the concepts of front-end and back-end. The front end refers to the interface that users directly interact with, and the back end refers to server-side programs. They interact with data through the HTTP protocol. When the front-end and back-end are separated, the front-end and back-end programs can be developed independently to implement business logic and interactive effects respectively, and data exchange.
