Heroku如何处理Django设置?

时间:2022-11-02 23:05:47

I'm trying to create a directory structure for a Django application that adheres to Heroku's recommended structure as shown at the bottom of this page. As you can see, they create a "settings" directory that contains individual settings files (e.g. dev.py) that are read depending on which tier the project is being deployed to.

我正在尝试为遵循Heroku推荐结构的Django应用程序创建一个目录结构,如本页底部所示。如您所见,他们创建了一个“设置”目录,其中包含根据项目部署到哪个层而读取的各个设置文件(例如dev.py)。

Now I would have expected that there would be a "settings.py" file at the same level as the settings directory that would import from one of the settings files in the settings directory. However, their graph doesn't show any such "settings.py" file, just the directory. How are they executing the appropriate settings file without a top-level settings.py file?

现在我原本期望会有一个“settings.py”文件与设置目录处于同一级别,该目录将从settings目录中的一个设置文件导入。但是,他们的图表不会显示任何此类“settings.py”文件,只显示目录。如果没有*settings.py文件,他们如何执行适当的设置文件?

Just as an experiment, I created a settings.py file that sits at the same level as the settings directory. The problem is that when I do "runserver," the init.py file in the settings directory causes an error: "'Settings' object has no 'ROOT_URLCONF'" I'm guess there is some sort of name collision because if I change the name of the settings directory to "conf" I don't get this error.

就像一个实验一样,我创建了一个settings.py文件,该文件与settings目录位于同一级别。问题是,当我执行“runserver”时,设置目录中的init.py文件会导致错误:“'设置'对象没有'ROOT_URLCONF'”我猜是有某种名称冲突,因为如果我改变设置目录的名称为“conf”我没有收到此错误。

Could the Heroku folks be assuming that the user is setting DJANGO_SETTINGS_MODULE somewhere? If not, how are they reading the correct settings file?

Heroku的人可以假设用户正在某处设置DJANGO_SETTINGS_MODULE吗?如果没有,他们如何阅读正确的设置文件?

Thanks.

1 个解决方案

#1


1  

When python is import module called settings it looks for either a file called settings.py or a directory (python module) that is called settings and contains __init__.py in it. This is just how python module system works.

当python是名为settings的导入模块时,它会查找名为settings.py的文件或名为settings的目录(python模块),并在其中包含__init__.py。这就是python模块系统的工作原理。

From there, you can decide which settings file to import inside your init file, importing stuff from insides like prod or dev. I guess that should be explained on that page somewhere, but I didn't find it at a glance.

从那里,您可以决定在init文件中导入哪个设置文件,从prod或dev等内部导入内容。我想这应该在某个地方的那个页面上解释,但我一眼就看不到它。

As for Heroku, it doesn't assume anything and goes by with django defaults. So the module settings is being imported.

至于Heroku,它不会假设任何东西,并且使用django默认值。因此正在导入模块设置。

Also, I will not argue against using dev/prod settings inside your project repo, but heroku gives you a really nice way to change config on the fly using ENV variables. You can find out more on their (very good) django tutorial.

另外,我不反对在项目仓库中使用dev / prod设置,但heroku为您提供了一种使用ENV变量动态更改配置的非常好的方法。你可以在他们(非常好的)django教程上找到更多信息。

#1


1  

When python is import module called settings it looks for either a file called settings.py or a directory (python module) that is called settings and contains __init__.py in it. This is just how python module system works.

当python是名为settings的导入模块时,它会查找名为settings.py的文件或名为settings的目录(python模块),并在其中包含__init__.py。这就是python模块系统的工作原理。

From there, you can decide which settings file to import inside your init file, importing stuff from insides like prod or dev. I guess that should be explained on that page somewhere, but I didn't find it at a glance.

从那里,您可以决定在init文件中导入哪个设置文件,从prod或dev等内部导入内容。我想这应该在某个地方的那个页面上解释,但我一眼就看不到它。

As for Heroku, it doesn't assume anything and goes by with django defaults. So the module settings is being imported.

至于Heroku,它不会假设任何东西,并且使用django默认值。因此正在导入模块设置。

Also, I will not argue against using dev/prod settings inside your project repo, but heroku gives you a really nice way to change config on the fly using ENV variables. You can find out more on their (very good) django tutorial.

另外,我不反对在项目仓库中使用dev / prod设置,但heroku为您提供了一种使用ENV变量动态更改配置的非常好的方法。你可以在他们(非常好的)django教程上找到更多信息。