Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Django with Heroku

Having problems trying to set a secret key inside an .env instead of the settings.py file

enter image description here

When I run the heroku config I get the secret key

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

(portfolio) PS C:\Users\arund\Desktop\Code\Django\portfolio-project> heroku config:get SECRET_KEY
value

When I run the following I get errors.

(portfolio) PS C:\Users\arund\Desktop\Code\Django\portfolio-project> heroku local
[OKAY] Loaded ENV .env File as KEY=VALUE Format
1:53:57 PM web.1 |  Traceback (most recent call last):
1:53:57 PM web.1 |    File "C:\Users\arund\Desktop\Code\Django\portfolio-project\manage.py", line 22, in <module>
1:53:57 PM web.1 |      main()
1:53:57 PM web.1 |    File "C:\Users\arund\Desktop\Code\Django\portfolio-project\manage.py", line 18, in main
1:53:57 PM web.1 |      execute_from_command_line(sys.argv)
1:53:57 PM web.1 |    File "C:\Users\arund\Desktop\Code\Django\portfolio-project\venv\lib\site-packages\django\core\management\__init__.py", line 419, in execute_from_command_line
1:53:57 PM web.1 |      utility.execute()
1:53:57 PM web.1 |    File "C:\Users\arund\Desktop\Code\Django\portfolio-project\venv\lib\site-packages\django\core\management\__init__.py", line 395, in execute
1:53:57 PM web.1 |      django.setup()
1:53:57 PM web.1 |    File "C:\Users\arund\Desktop\Code\Django\portfolio-project\venv\lib\site-packages\django\__init__.py", line 24, in setup
1:53:57 PM web.1 |      apps.populate(settings.INSTALLED_APPS)
1:53:57 PM web.1 |    File "C:\Users\arund\Desktop\Code\Django\portfolio-project\venv\lib\site-packages\django\apps\registry.py", line 122, in populate
1:53:57 PM web.1 |      app_config.ready()
1:53:57 PM web.1 |    File "C:\Users\arund\Desktop\Code\Django\portfolio-project\venv\lib\site-packages\django\contrib\admin\apps.py", line 27, in ready
1:53:57 PM web.1 |      self.module.autodiscover()
1:53:57 PM web.1 |    File "C:\Users\arund\Desktop\Code\Django\portfolio-project\venv\lib\site-packages\django\contrib\admin\__init__.py", line 24, in autodiscover
1:53:57 PM web.1 |      autodiscover_modules('admin', register_to=site)
1:53:57 PM web.1 |    File "C:\Users\arund\Desktop\Code\Django\portfolio-project\venv\lib\site-packages\django\utils\module_loading.py", line 47, in autodiscover_modules
1:53:57 PM web.1 |      import_module('%s.%s' % (app_config.name, module_to_search))
1:53:57 PM web.1 |    File "C:\Python39\lib\importlib\__init__.py", line 127, in import_module
1:53:57 PM web.1 |      return _bootstrap._gcd_import(name[level:], package, level)
1:53:57 PM web.1 |    File "<frozen importlib._bootstrap>", line 1030, in _gcd_import
1:53:57 PM web.1 |    File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
1:53:57 PM web.1 |    File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
1:53:57 PM web.1 |    File "<frozen importlib._bootstrap>", line 680, in _load_unlocked
1:53:57 PM web.1 |    File "<frozen importlib._bootstrap_external>", line 790, in exec_module
1:53:57 PM web.1 |    File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed
1:53:57 PM web.1 |    File "C:\Users\arund\Desktop\Code\Django\portfolio-project\venv\lib\site-packages\django\contrib\auth\admin.py", line 6, in <module>
1:53:57 PM web.1 |      from django.contrib.auth.forms import (
1:53:57 PM web.1 |    File "C:\Users\arund\Desktop\Code\Django\portfolio-project\venv\lib\site-packages\django\contrib\auth\forms.py", line 11, in <module>
1:53:57 PM web.1 |      from django.contrib.auth.tokens import default_token_generator
1:53:57 PM web.1 |    File "C:\Users\arund\Desktop\Code\Django\portfolio-project\venv\lib\site-packages\django\contrib\auth\tokens.py", line 117, in <module>
1:53:57 PM web.1 |      default_token_generator = PasswordResetTokenGenerator()
1:53:57 PM web.1 |    File "C:\Users\arund\Desktop\Code\Django\portfolio-project\venv\lib\site-packages\django\contrib\auth\tokens.py", line 18, in __init__
1:53:57 PM web.1 |      self.secret = self.secret or settings.SECRET_KEY
1:53:57 PM web.1 |    File "C:\Users\arund\Desktop\Code\Django\portfolio-project\venv\lib\site-packages\django\conf\__init__.py", line 90, in __getattr__
1:53:57 PM web.1 |      raise ImproperlyConfigured("The SECRET_KEY setting must not be empty.")
1:53:57 PM web.1 |  django.core.exceptions.ImproperlyConfigured: The SECRET_KEY setting must not be empty.
[DONE] Killing all processes with signal  SIGINT
1:53:57 PM web.1 Exited with exit code null

>Solution :

Django doesn’t magically apply environment variables to your settings. You need to do that yourself, e.g. via something like this in your settings.py:

import os

SECRET_KEY = os.getenv("SECRET_KEY", default="dev key")

In development, you can use a .env file to populate your environment, but that file should not be committed to version control. Ignore it, and use Heroku’s config vars instead. They will be injected into your application’s environment.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading