且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

ImproperlyConfigured:您必须在访问设置之前定义环境变量 DJANGO_SETTINGS_MODULE 或调用 settings.configure()

更新时间:2022-04-05 08:50:37

我认为 DJANGO_SETTINGS_MODULE 必须以某种方式设置,所以我查看了 文档(链接已更新)并找到:

I figured that the DJANGO_SETTINGS_MODULE had to be set some way, so I looked at the documentation (link updated) and found:

export DJANGO_SETTINGS_MODULE=mysite.settings

如果您在 heroku 上运行服务器,这还不够,您也需要在那里指定它.像这样:

Though that is not enough if you are running a server on heroku, you need to specify it there, too. Like this:

heroku config:set DJANGO_SETTINGS_MODULE=mysite.settings --account <your account name> 

在我的具体情况下,我运行了这两个,一切都解决了:

In my specific case I ran these two and everything worked out:

export DJANGO_SETTINGS_MODULE=nirla.settings
heroku config:set DJANGO_SETTINGS_MODULE=nirla.settings --account personal

编辑

我还想指出,每次关闭或重新启动虚拟环境时都必须重新执行此操作.相反,您应该通过转到 venv/bin/activate 并添加以下行来自动化该过程:set DJANGO_SETTINGS_MODULE=mysite.settings 到代码的底部.从现在开始,每次激活虚拟环境时,您都将使用该应用的设置.

I would also like to point out that you have to re-do this every time you close or restart your virtual environment. Instead, you should automate the process by going to venv/bin/activate and adding the line: set DJANGO_SETTINGS_MODULE=mysite.settings to the bottom of the code. From now on every time you activate the virtual environment, you will be using that app's settings.