Skip to main content
 首页 » 编程设计

python之设置 DATABASE_URL 环境变量

2024年12月31日7think

我正在尝试通过查看两勺 django 来创建我自己的项目结构。项目结构看起来像这样

.环境

.本地

  .django 
 
  .postgres 

.生产

 .django 
 
 .postgres 

在本地的postgres中,列出了以下项目

POSTGRES_HOST=postgres 
POSTGRES_PORT=5432 
POSTGRES_DB=marketing 
POSTGRES_USER=username 
POSTGRES_PASSWORD=password 
DATABASE_URL=postgres://username:password@localhost:5432/marketing 

设置文件分为3个部分base.py、local.py和production.py

在 base.py 中,我配置了 DATABASES 键如下

DATABASES = { 
    'default': env.db('DATABASE_URL'), 
} 

但是我收到一个错误

django.core.exceptions.ImproperlyConfigured: Set the DATABASE_URL environment variable

尽管我在 .envs/.local/.postgres 中有 DATABASE_URL,但我遇到了上述错误。为什么会这样?

请您参考如下方法:

在 .myfilename 中的变量之前添加 export。

export POSTGRES_HOST=postgres 
export POSTGRES_PORT=5432 
export POSTGRES_DB=marketing 
export POSTGRES_USER=username 
export POSTGRES_PASSWORD=password 
export DATABASE_URL=postgres://username:password@localhost:5432/marketing 

然后做

源.myfilename