INSTALLED_APPS中应用程序订单的重要性 [英] Importance of apps orders in INSTALLED_APPS
问题描述
INSTALLED_APPS
中的应用顺序重要吗?我之所以这样问,是因为我有 settings
文件夹,其中有两个 settings
文件: base.py
和 production.py
,然后将所有设置放入 base.py
,然后放入 production.py
,我有:
Is the order of apps in INSTALLED_APPS
important? I ask it because I have settings
folder with two settings
files: base.py
and production.py
and I put all my settings in base.py
and then in production.py
, I have:
from base import *
然后我覆盖一些设置。
也在我的 base.py
中,将 INSTALLED_APPS
列为一个列表,而不是一个元组。因为我想删除某些用于生产
设置的应用。
在 production.py
中,我要写:
and then I override some settings.
Also in my base.py
, I make INSTALLED_APPS
a list, not a tuple. Because I want to remove some apps for production
settings.
In production.py
I want to write:
NOT_USED_APPS = ['debut_toolbar', 'other_odd_app',]
INSTALLED_APPS = list(set(INSTALLED_APPS) - set(NOT_USED_APPS))
在这种情况下, INSTALLED_APPS
中应用的顺序与 base.py $ c $中的不同c>
In this case, the order of apps in INSTALLED_APPS
is not like in base.py
推荐答案
是的,顺序非常重要。
来自 INSTALLED_APPS
设置:
当多个应用程序提供同一$ b $的不同版本时b资源(模板,静态文件,管理命令,翻译)中,在
INSTALLED_APPS
中首先列出的
应用程序具有优先权。
When several applications provide different versions of the same resource (template, static file, management command, translation), the application listed first in
INSTALLED_APPS
has precedence.
Example-1模板
django.template.loaders.app_directories.Loader
如果在 DjangoTemplates
后端中启用了此模板加载器 TEMPLATES
设置,或者如果已将其作为加载器参数传递给Engine,则它将从文件系统上的Django应用程序加载模板。
If this template loader is enabled in your DjangoTemplates
backend in the TEMPLATES
setting or if you have passed it as a loaders argument to Engine, then it loads templates from Django apps on the filesystem.
对于 INSTALLED_APPS
中的每个应用程序,加载程序都会查找模板子目录。如果该目录存在,则Django将在其中查找模板。
For each app in INSTALLED_APPS
, the loader looks for a templates subdirectory. If the directory exists, Django will look for templates in there.
在我的项目中,我将 INSTALLED_APPS
定义为:
Lets say in my project, i have defined INSTALLED_APPS
as:
INSTALLED_APPS = ('myproject.app1', 'myproject.app2')
现在,我想获取模板 some_template.html
。然后 get_template('some_template.html')
将在这些目录中查找 some_template.html
订单:
Now, i want to get the template some_template.html
. Then get_template('some_template.html')
will look for some_template.html
in these directories, in this order:
/path/to/myproject/app1/templates/ # checks here first
/path/to/myproject/app2/templates/ # Then checks here
然后它将使用
从该部分:
The
INSTALLED_APPS
的顺序很重要!
示例2:翻译
Django将以下算法用于发现翻译:
Django applies the following algorithm for discovering translations:
-
LOCALE_PATHS
中列出的目录具有最高优先级,而出现的目录优先级较高。 - 然后,它查找并使用列出的每个已安装应用中是否存在
locale
目录在INSTALLED_APPS
中。 首先出现的优先级高于随后出现的优先级。 - 最后,
django / conf /中Django提供的基础翻译
- The directories listed in
LOCALE_PATHS
have the highest precedence, with the ones appearing first having higher precedence than the ones appearing later. - Then, it looks for and uses if it exists a
locale
directory in each of the installed apps listed inINSTALLED_APPS
. The ones appearing first have higher precedence than the ones appearing later. - Finally, the Django-provided base translation in
django/conf/locale
is used as a fallback.
我们可以看到订单在这里也很重要。
We can see that order is important here also.
示例3管理命令:
当多个应用程序提供具有相同
名称的管理命令时,Django从INSTALLED_APPS
中第一个
的应用程序中加载命令。先前的版本从最后出现的
应用程序中加载了命令。
When several applications provide management commands with the same name, Django loads the command from the application that comes first in
INSTALLED_APPS
. Previous versions loaded the command from the application that came last.
这使管理命令的发现与依赖于Django的
的其他部分保持一致按 INSTALLED_APPS
的顺序,例如静态
文件,模板和翻译。
This brings discovery of management commands in line with other parts
of Django that rely on the order of INSTALLED_APPS
, such as static
files, templates, and translations.
这篇关于INSTALLED_APPS中应用程序订单的重要性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!