In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains "Django dynamic how to set static file path", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let Xiaobian take you to learn "Django dynamic how to set static file path"!
preface
Django projects need to set the path of various files in the settings.py file, such as the path of media files, the path of static files, and the path of template files. However, for every developer, Python virtual environments can be stored anywhere, so the absolute path of every developer file is not exactly the same. In order to avoid the abnormal situation caused by different file locations in different running environments, this problem can be solved by dynamically setting the path.
Modify the path-related settings according to the project, instead of hardcoding the file path into the file, modify the base.py file, as follows:
1. Modify BASE_DIR:
Before modification:
BASE_DIR = Path(__file__).resolve().parent.parent
the modified content
BASE_DIR = Path(__file__).resolve().parent.parent.parent
Second, modify the template file path (DIRS value in TEMPLATES):
Code example before modifying template path
Example after modifying template path code
Note: Since there is no os module imported by default in the base.py file, you need to import the module at the beginning of the base.py file, as follows:
Import os module example
Third, modify the international file path, because LOCALE_PATHS does not exist by default, you need to add it at the appropriate location, as shown in the following figure:
Example Internationalized File Path Code
4. Modify the public file path, as shown in the following figure:
Code example after modifying public static file path
Note: STATICFILES_DIRS is a public folder, because some static files are not unique to an app, so you can put it in a public folder for easy management. Note that creating a common folder of static files is only an easy to manage practice, but it is not necessary, the app is able to apply static files across apps, because eventually all static files will exist in STATIC_ROOT. Because a project may contain multiple apps, you can create a site folder (specifically named according to the app name) in STATICFILES_DIRS to separate the static files of each app, which is equivalent to the same role as the namespace (for specific structure, please refer to Django project construction and corresponding directory introduction).
5. Modify the STATIC_ROOT file path, as shown in the following figure:
Code example after modifying STATIC_ROOT
Note: STATIC_ROOT is needed when deploying django project. It collects all static files. After executing python manage.py collectstatic, copy the static files to the directory specified by STATIC_ROOT.
VI. Modify the multimedia path, as shown in the following figure:
Code example after modifying multimedia path
Supplement: STATIC_URL indicates the "root path" of the browser to access static resources, that is, the path is the value displayed in the browser address bar, and its essence is to map STATIC_ROOT with the specific content of STATIC_URL. Because users cannot enter the server local address to access static resources when browsing, they need to access them in the form of urls.
At this point, I believe that everyone has a deeper understanding of "Django dynamic how to set static file path", may wish to actually operate it! Here is the website, more related content can enter the relevant channels for inquiry, pay attention to us, continue to learn!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.