


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!

TomergelistsinPython,youcanusethe operator,extendmethod,listcomprehension,oritertools.chain,eachwithspecificadvantages:1)The operatorissimplebutlessefficientforlargelists;2)extendismemory-efficientbutmodifiestheoriginallist;3)listcomprehensionoffersf

In Python 3, two lists can be connected through a variety of methods: 1) Use operator, which is suitable for small lists, but is inefficient for large lists; 2) Use extend method, which is suitable for large lists, with high memory efficiency, but will modify the original list; 3) Use * operator, which is suitable for merging multiple lists, without modifying the original list; 4) Use itertools.chain, which is suitable for large data sets, with high memory efficiency.

Using the join() method is the most efficient way to connect strings from lists in Python. 1) Use the join() method to be efficient and easy to read. 2) The cycle uses operators inefficiently for large lists. 3) The combination of list comprehension and join() is suitable for scenarios that require conversion. 4) The reduce() method is suitable for other types of reductions, but is inefficient for string concatenation. The complete sentence ends.

PythonexecutionistheprocessoftransformingPythoncodeintoexecutableinstructions.1)Theinterpreterreadsthecode,convertingitintobytecode,whichthePythonVirtualMachine(PVM)executes.2)TheGlobalInterpreterLock(GIL)managesthreadexecution,potentiallylimitingmul

Key features of Python include: 1. The syntax is concise and easy to understand, suitable for beginners; 2. Dynamic type system, improving development speed; 3. Rich standard library, supporting multiple tasks; 4. Strong community and ecosystem, providing extensive support; 5. Interpretation, suitable for scripting and rapid prototyping; 6. Multi-paradigm support, suitable for various programming styles.

Python is an interpreted language, but it also includes the compilation process. 1) Python code is first compiled into bytecode. 2) Bytecode is interpreted and executed by Python virtual machine. 3) This hybrid mechanism makes Python both flexible and efficient, but not as fast as a fully compiled language.

Useaforloopwheniteratingoverasequenceorforaspecificnumberoftimes;useawhileloopwhencontinuinguntilaconditionismet.Forloopsareidealforknownsequences,whilewhileloopssuitsituationswithundeterminediterations.

Pythonloopscanleadtoerrorslikeinfiniteloops,modifyinglistsduringiteration,off-by-oneerrors,zero-indexingissues,andnestedloopinefficiencies.Toavoidthese:1)Use'i


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

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Dreamweaver Mac version
Visual web development tools

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function
