Django + Heroku:访问 Heroku 的配置变量时出现 KeyError



我正试图通过以下页面从Heroku的配置变量中获取所有环境变量。因此,在settings.py中,我写道:

import os
from dotenv import load_dotenv
from boto.s3.connection import S3Connection
# ...
load_dotenv(verbose=True)
# ...
s3 = S3Connection(os.environ['SECRET_KEY'],
os.environ['DATABASE_URL'],
os.environ['SENDGRID_API_KEY'])
SECRET_KEY = os.getenv('SECRET_KEY')
# ...
if os.environ.get('DEBUG') is None:
DEBUG = False
else:
DEBUG = True
# ...
# Sending emails
SENDGRID_API_KEY = os.getenv('SENDGRID_API_KEY')
EMAIL_HOST = 'smtp.sendgrid.net'
EMAIL_HOST_USER = 'apikey'
EMAIL_HOST_PASSWORD = SENDGRID_API_KEY
EMAIL_PORT = 587
EMAIL_USE_TLS = True
# ...

我想要一个包含这些变量的本地.env,并在生产中使用Heroku的配置变量。但当我部署时,我会得到一个Application Error,这是heroku logs --tail:的结果

2020-02-19T04:09:27.730183+00:00 app[web.1]: File "/app/smart_toilet_system/settings.py", line 34, in <module>
2020-02-19T04:09:27.730183+00:00 app[web.1]: os.environ['SENDGRID_API_KEY'])
2020-02-19T04:09:27.730183+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.7/site-packages/boto/s3/connection.py", line 194, in __init__
2020-02-19T04:09:27.730184+00:00 app[web.1]: validate_certs=validate_certs, profile_name=profile_name)
2020-02-19T04:09:27.730184+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.7/site-packages/boto/connection.py", line 502, in __init__
2020-02-19T04:09:27.730185+00:00 app[web.1]: self.port = PORTS_BY_SECURITY[is_secure]
2020-02-19T04:09:27.730185+00:00 app[web.1]: KeyError: '<SENDGRID_API_KEY>'
2020-02-19T04:09:27.730911+00:00 app[web.1]: [2020-02-19 04:09:27 +0000] [10] [INFO] Worker exiting (pid: 10)
2020-02-19T04:09:27.838786+00:00 app[web.1]: [2020-02-19 04:09:27 +0000] [4] [INFO] Shutting down: Master
2020-02-19T04:09:27.839030+00:00 app[web.1]: [2020-02-19 04:09:27 +0000] [4] [INFO] Reason: Worker failed to boot.
2020-02-19T04:09:28.081341+00:00 heroku[web.1]: State changed from up to crashed
2020-02-19T04:09:28.062132+00:00 heroku[web.1]: Process exited with status 3

导致此错误的原因是什么?这与转义特殊字符有关吗?

我认为您无法访问它们,因为它们不在该环境中(这意味着它们不在您的正常环境中(。为了访问这些.env-config变量,您可以使用python解耦(安装它(并访问这些变量,如下所示:

from decouple import config
s3 = S3Connection(config('SECRET_KEY'),
config('DATABASE_URL'),
config('SENDGRID_API_KEY'))

我希望这会让你难过

相关内容

  • 没有找到相关文章

最新更新