Python 它是如何工作的,Django INSTALLED_APPS 的命名约定?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34377237/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How does it work, the naming convention for Django INSTALLED_APPS?
提问by losee
The tutorial on the site creates an app named polls. It's using django 1.9, so in the INSTALLED_APPS it is:
该网站上的教程创建了一个名为 polls 的应用程序。它使用 django 1.9,所以在 INSTALLED_APPS 中它是:
polls.apps.PollsConfig
I'm watching a tutorial he names the app newsletter and in INSTALLED_APPS he has
我正在看一个教程,他将应用通讯命名为 INSTALLED_APPS
newsletter
he's using 1.8, though. I am using 1.9. I've watched other tutorials and they also just add a name without dots in the syntax as he does. I realize things may be different, that's understood. To be clear if I named my app dogs,. in the installed apps it would be named like this
不过他用的是 1.8。我正在使用 1.9。我看过其他教程,他们也像他一样在语法中添加了一个没有点的名称。我意识到事情可能会有所不同,这是可以理解的。要清楚我是否将我的应用程序命名为狗,。在已安装的应用程序中,它会像这样命名
dogs.apps.DogsConfig
or if it was tree it would be
或者如果它是树,那就是
tree.apps.TreeConfig
Is that how the naming convention goes? also I would assume things would get shorter in newer versions and more convenient. so to go from just adding
这就是命名约定的方式吗?我也认为在新版本中事情会变得更短,更方便。所以从只是添加
newsletter,
to having to type out
不得不打字
polls.apps.PollsConfig
seems weird to me. But I'm new so I maybe missing something. Any and all advice is welcome
对我来说似乎很奇怪。但我是新手,所以我可能会遗漏一些东西。欢迎任何和所有建议
采纳答案by rodrigo
That is the Application Configurationfeature, new to Django 1.7.
那是Django 1.7 新增的应用程序配置功能。
Basically, now you can list in INSTALLED_APPS
either the module that contains the application or a class that derives from django.apps.AppConfig
and defines the behavior of the application.
基本上,现在您可以列出INSTALLED_APPS
包含应用程序的模块或派生自django.apps.AppConfig
并定义应用程序行为的类。
This feature provides several advantages:
此功能提供了几个优点:
- Apps can be configured more easily, and even subclassed for customization.
- You can have several apps in the same module.
- 应用程序可以更轻松地配置,甚至可以进行子类化以进行自定义。
- 您可以在同一个模块中拥有多个应用程序。
Application modules can define the special module variable default_app_config
to specify the name of their AppConfig
, so that they can use the new features without having to specify the full name of that class in INSTALLED_APPS
. But this is a backwards compatibility feature and new applications are recommended to write the full AppConfig
name.
应用程序模块可以定义特殊的模块变量default_app_config
来指定它们的名称AppConfig
,这样它们就可以使用新功能而不必在INSTALLED_APPS
. 但这是一个向后兼容功能,建议新应用程序写全AppConfig
名。
Anyway, most django/contrib
apps use that default_app_config
, for compatibility with old configurations. See for example the file django/contrib/messages/__init__.py
is just:
无论如何,大多数django/contrib
应用程序都使用它default_app_config
,以便与旧配置兼容。参见例如文件django/contrib/messages/__init__.py
只是:
from django.contrib.messages.api import *
from django.contrib.messages.constants import *
default_app_config = 'django.contrib.messages.apps.MessagesConfig'
So, adding it up, per OP request:
因此,根据 OP 请求将其加起来:
- If you add in
INSTALLED_APPS
the typenamefoo.apps.FooConfig
, then that class will be used to setup thefoo
app, 1.7 style (recommended). If you add in
INSTALLED_APPS
the plain namefoo
, then:- if there is a variable
foo.default_app_config
this class will be used to setup thefoo
app, 1.7 style. Most (all?) the standard Django apps have this variable, so that you don't need to change yourINSTALLED_APPS
when you upgrade from Django-1.6 to Django-1.7. - if there is not such a variable, then the 1.6 style application will be used, with default values for the advanced configuration options.
- if there is a variable
- 如果您添加
INSTALLED_APPS
typenamefoo.apps.FooConfig
,则该类将用于设置foo
应用程序,1.7 样式(推荐)。 如果您添加
INSTALLED_APPS
纯名称foo
,则:- 如果有一个变量,
foo.default_app_config
这个类将用于设置foo
应用程序,1.7 样式。大多数(全部?)标准 Django 应用程序都有这个变量,所以INSTALLED_APPS
当你从 Django-1.6 升级到 Django-1.7 时不需要改变你的。 - 如果没有这样的变量,则将使用 1.6 样式的应用程序,高级配置选项具有默认值。
- 如果有一个变量,
回答by Pom Skipper
In the setup.py ,under the Installed apps just add app_name
like
在 setup.py 中,在 Installed apps 下添加app_name
像
INSTALLED_APPS = [
'polls', # <--- here
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
]
回答by vildhjarta
While I was searching about INSTALLED_APPSconstant like you I have seen this explanation on documents:
当我像你一样搜索INSTALLED_APPS常量时,我在文档中看到了这个解释:
A list of strings designating all applications that are enabled in this Django installation. Each string should be a dotted Python path to:
指定在此 Django 安装中启用的所有应用程序的字符串列表。每个字符串应该是一个带点的 Python 路径:
- an application configuration class (preferred), or
- a package containing an application.
- 应用程序配置类(首选),或
- 包含应用程序的包。
The second bullet is explaining that you can use your application folder for resolving the existing settings automatically. The first option can be also used for activate your application. But the most important thing is that it is preferred one.
第二个项目符号说明您可以使用应用程序文件夹自动解析现有设置。第一个选项也可用于激活您的应用程序。但最重要的是它是首选之一。
If you want to go with the preferred one first you have to create app.py in your new application folder then set like this:
如果你想先使用首选的,你必须在你的新应用程序文件夹中创建 app.py 然后像这样设置:
# my_new_app/apps.py
from django.apps import AppConfig
class MyNewAppConfig(AppConfig):
name = 'my_new_app'
verbose_name = "My Brand New Application"
# my_new_app/__init__.py
default_app_config = 'my_new_app.apps.MyNewAppConfig'
Why the first one is preferred? Because I think of that it is explicit.
为什么首选第一个?因为我认为它是明确的。