gpt4 book ai didi

django - 如何在 Django 设置中访问 Heroku 配置变量

转载 作者:行者123 更新时间:2023-12-05 04:45:46 24 4
gpt4 key购买 nike

我正在尝试将我的代码推送到 Heroku 上,我已经使用 environ 包隐藏了我的 key ,但现在 Heroku 无法访问它,因为我使用 忽略了我的 .env 文件gitignore,我已经阅读了 Heroku 中的配置变量,但我无法理解如何让 Django 访问这些值

    import os
import environ
# from .secret import key

env = environ.Env()
environ.Env.read_env()


SECRET_KEY = env('KEY',default=env('SECRET_KEY'))

更新代码在日志中,它给我这样的错误

warnings.warn(
Traceback (most recent call last):
File "/app/.heroku/python/lib/python3.8/site-packages/environ/environ.py", line 277, in get_value
value = self.ENVIRON[var]
File "/app/.heroku/python/lib/python3.8/os.py", line 675, in __getitem__
raise KeyError(key) from None
KeyError: 'SECRET_KEY'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "manage.py", line 22, in <module>
main()
File "manage.py", line 18, in main
execute_from_command_line(sys.argv)
File "/app/.heroku/python/lib/python3.8/site-packages/django/core/management/__init__.py", line 419, in execute_from_command_line
utility.execute()
File "/app/.heroku/python/lib/python3.8/site-packages/django/core/management/__init__.py", line 413, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/app/.heroku/python/lib/python3.8/site-packages/django/core/management/__init__.py", line 244, in fetch_command
settings.INSTALLED_APPS
File "/app/.heroku/python/lib/python3.8/site-packages/django/conf/__init__.py", line 82, in __getattr__
self._setup(name)
File "/app/.heroku/python/lib/python3.8/site-packages/django/conf/__init__.py", line 69, in _setup
self._wrapped = Settings(settings_module)
File "/app/.heroku/python/lib/python3.8/site-packages/django/conf/__init__.py", line 170, in __init__
mod = importlib.import_module(self.SETTINGS_MODULE)
File "/app/.heroku/python/lib/python3.8/importlib/__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
File "<frozen importlib._bootstrap>", line 991, in _find_and_load
File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 783, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "/tmp/build_067d1d45/food_deliveryapp/settings.py", line 22, in <module>
SECRET_KEY = os.environ.get('KEY',env('SECRET_KEY'))
File "/app/.heroku/python/lib/python3.8/site-packages/environ/environ.py", line 127, in __call__
return self.get_value(var, cast=cast, default=default, parse_default=parse_default)
File "/app/.heroku/python/lib/python3.8/site-packages/environ/environ.py", line 281, in get_value
raise ImproperlyConfigured(error_msg)
django.core.exceptions.ImproperlyConfigured: Set the SECRET_KEY environment variable
! Error while running '$ python manage.py collectstatic --noinput'.
See traceback above for details.
You may need to update application code to resolve this error.
Or, you can disable collectstatic for this application:
$ heroku config:set DISABLE_COLLECTSTATIC=1
https://devcenter.heroku.com/articles/django-assets
! Push rejected, failed to compile Python app.
! Push failed

最佳答案

.env 中的每个变量创建一个 Config Var , 确保名称是大写的。

在您的代码中,您可以通过 environ(包含所有环境变量的映射)访问它们:

my_secret = (os.environ.get("MY_SECRET", 'dev default value')

关于django - 如何在 Django 设置中访问 Heroku 配置变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69054762/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com