Heroku - 将其声明为envvar或定义默认值

时间:2018-03-11 05:29:29

标签: django python-3.x heroku heroku-toolbelt decouple

我正在尝试在本地运行我的Heroku应用,并按照here的说明进行操作。

当我尝试运行以下命令时:

python manage.py collectstatic

我发现了这个错误:

Traceback (most recent call last):
File "manage.py", line 15, in <module>
execute_from_command_line(sys.argv)
File "C:\Python36-32\lib\site-packages\django\core\management\__init__.py", line 371, in execute_from_command_line
utility.execute()
File "C:\Python36-32\lib\site-packages\django\core\management\__init__.py", line 317, in execute
settings.INSTALLED_APPS
File "C:\Python36-32\lib\site-packages\django\conf\__init__.py", line 56, in __getattr__
self._setup(name)
File "C:\Python36-32\lib\site-packages\django\conf\__init__.py", line 43, in _setup
self._wrapped = Settings(settings_module)
File "C:\Python36-32\lib\site-packages\django\conf\__init__.py", line 106, in __init__
mod = importlib.import_module(self.SETTINGS_MODULE)
File "C:\Python36-32\lib\importlib\__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 994, in _gcd_import
File "<frozen importlib._bootstrap>", line 971, in _find_and_load
File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 665, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 678, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "C:\Users\robina\Ubelt-Dorm\ph_dorms\settings.py", line 25, in <module>
SECRET_KEY = config('SECRET_KEY')
File "C:\Python36-32\lib\site-packages\decouple.py", line 197, in __call__
return self.config(*args, **kwargs)
File "C:\Python36-32\lib\site-packages\decouple.py", line 85, in __call__
return self.get(*args, **kwargs)
File "C:\Python36-32\lib\site-packages\decouple.py", line 70, in get
raise UndefinedValueError('{} not found. Declare it as envvar or define a default value.'.format(option))
decouple.UndefinedValueError: SECRET_KEY not found. Declare it as envvar or define a default value.

SECRET_KEY已在Heroku上声明为配置变量。

enter image description here

我已经联系了Heroku,但仍然没有回应,所以也许有人可以在这之前找到解决这个错误的方法。

我想使用与网络相同的版本在本地运行此应用,因此在SECRET_KEY上添加settings.py不适合我。

我的settings.py配置方式如下:

from decouple import config
SECRET_KEY = config('SECRET_KEY')

2 个答案:

答案 0 :(得分:2)

您可以通过将本地环境变量添加到.env文件,然后通过heroku local启动开发服务器来设置本地环境变量。这会将所有NAME=value行从.env转换为env vars,然后将其提供给您的应用。然后,您可以将.env添加到.gitignore,因为只有在本地计算机上才需要这样做。有关详细信息,请参阅aws-cli documentation

答案 1 :(得分:1)

您可以使用Heroku的命令行工具设置环境变量,如下所示:

heroku config:set SECRET_KEY='Your secret key'