前提・実現したいこと
DjangoでSECRET_KEYを隠したいと思っています。
SECRET_KEYを置いておく、local_settings.pyを作成して、settinds.pyに
from .local_settings import *
でインポートし、Herokuでマイグレーションするとエラーが発生してしまいました。
発生している問題・エラーメッセージ
heroku run python manage.py makemigrations File "/app/config/settings.py", line 18, in <module> from .local_settings import * ModuleNotFoundError: No module named 'config.local_settings' 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.6/site-packages/django/core/management/__init__.py", line 401, in execute_from_command_line utility.execute() File "/app/.heroku/python/lib/python3.6/site-packages/django/core/management/__init__.py", line 395, in execute self.fetch_command(subcommand).run_from_argv(self.argv) File "/app/.heroku/python/lib/python3.6/site-packages/django/core/management/base.py", line 343, in run_from_argv connections.close_all() File "/app/.heroku/python/lib/python3.6/site-packages/django/db/utils.py", line 232, in close_all for alias in self: File "/app/.heroku/python/lib/python3.6/site-packages/django/db/utils.py", line 226, in __iter__ return iter(self.databases) File "/app/.heroku/python/lib/python3.6/site-packages/django/utils/functional.py", line 48, in __get__ res = instance.__dict__[self.name] = self.func(instance) File "/app/.heroku/python/lib/python3.6/site-packages/django/db/utils.py", line 153, in databases self._databases = settings.DATABASES File "/app/.heroku/python/lib/python3.6/site-packages/django/conf/__init__.py", line 83, in __getattr__ self._setup(name) File "/app/.heroku/python/lib/python3.6/site-packages/django/conf/__init__.py", line 70, in _setup self._wrapped = Settings(settings_module) File "/app/.heroku/python/lib/python3.6/site-packages/django/conf/__init__.py", line 177, in __init__ mod = importlib.import_module(self.SETTINGS_MODULE) File "/app/.heroku/python/lib/python3.6/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 "/app/config/settings.py", line 18, in <module> from .local_settings import * ModuleNotFoundError: No module named 'config.local_settings'
該当のソースコード
#settings.pyより from .local_settings import * #local_settingsには SECRET_KEY = 'hogehoge' と書いてあります。
試したこと
settings.pyに以下のコードを追加しました。 import json # Normally you should not import ANYTHING from Django directly # into your settings, but ImproperlyConfigured is an exception. from django.core.exceptions import ImproperlyConfigured # JSON-based secrets module with open("secrets.json") as f: secrets = json.loads(f.read()) def get_secret(setting, secrets=secrets): """Get the secret variable or return explicit exception.""" try: return secrets[setting] except KeyError: error_msg = "Set the {0} environment variable".format(setting) raise ImproperlyConfigured(error_msg) SECRET_KEY = get_secret("SECRET_KEY") secrets.jsonを作成して以下のコードを書きました。 { "FILENAME": "secrets.json", "SECRET_KEY": "hogehoge" }
補足情報(FW/ツールのバージョンなど)
python-3.6.12
requirements.txtより
autopep8==1.5.4
dj-database-url==0.5.0
flake8==3.8.4
mccabe==0.6.1
pycodestyle==2.6.0
pyflakes==2.2.0
pytz==2020.4
sqlparse==0.4.1
toml==0.10.2
whitenoise==5.2.0
psycopg2==2.8.6
django
gunicorn
django-heroku
requests
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。