1
votes

I'm trying to deploy my Django project using Heroku, but now stuck with the empty SECRET_KEY problem when I run the heroku run python manage.py syncdb command:

Traceback (most recent call last):

File "manage.py", line 10, in execute_from_command_line(sys.argv)

File "/app/.heroku/python/lib/python2.7/site-packages/django/core/management/init.py", line 338, in execute_from_command_line utility.execute()

File "/app/.heroku/python/lib/python2.7/site-packages/django/core/management/init.py", line 330, in execute self.fetch_command(subcommand).run_from_argv(self.argv)

File "/app/.heroku/python/lib/python2.7/site-packages/django/core/management/init.py", line 190, in fetch_command klass = load_command_class(app_name, subcommand)

File "/app/.heroku/python/lib/python2.7/site-packages/django/core/management/init.py", line 40, in load_command_class module = import_module('%s.management.commands.%s' % (app_name, name))

File "/app/.heroku/python/lib/python2.7/importlib/init.py", line 37, in import_module import(name)

File "/app/.heroku/python/lib/python2.7/site-packages/django/core/management/commands/syncdb.py", line 4, in from django.contrib.auth import get_user_model

File "/app/.heroku/python/lib/python2.7/site-packages/django/contrib/auth/init.py", line 7, in from django.middleware.csrf import rotate_token

File "/app/.heroku/python/lib/python2.7/site-packages/django/middleware/csrf.py", line 14, in from django.utils.cache import patch_vary_headers

File "/app/.heroku/python/lib/python2.7/site-packages/django/utils/cache.py", line 26, in from django.core.cache import caches

File "/app/.heroku/python/lib/python2.7/site-packages/django/core/cache/init.py", line 34, in if DEFAULT_CACHE_ALIAS not in settings.CACHES:

File "/app/.heroku/python/lib/python2.7/site-packages/django/conf/init.py", line 48, in getattr self._setup(name)

File "/app/.heroku/python/lib/python2.7/site-packages/django/conf/init.py", line 44, in _setup self._wrapped = Settings(settings_module)

File "/app/.heroku/python/lib/python2.7/site-packages/django/conf/init.py", line 113, in init raise ImproperlyConfigured("The SECRET_KEY setting must not be empty.") django.core.exceptions.ImproperlyConfigured: The SECRET_KEY setting must not be empty.

However, running python manage.py syncdb is totally fine. In my settings.py file, I even wrote the SECRET_EKY explicitly as:

    import os

    BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

    SECRET_KEY = '*$_99!t5u&#w&9boeq()=obq@rjdsfasdfa#8hp_5s%h-mh(hk'`

I still got the same problem. I wondered if the environment variable DJANGO_SETTINGS_MODULE was missing, but running heroku config gave me (I replaced the information with xxx):

DATABASE_URL: xxx

DJANGO_SETTINGS_MODULE: xxx

SECRET_KEY: xxx

It seems that the relevant environment variables do exist. I really hope you could give me some suggestions. Thank you very much in advance.

2
First of all you should change your secret key now. Second, please give a description of the error because no one can guess what the error might be.enpenax
@enpenax I have added more information, please let me know if there is anything else I need to provide. Thank you.Yuan Tian
are you sure your settings file is found and loaded?enpenax
This most probably is related to django not able to find its settings file and not specifically related to the SECRET_KEY setting. The problem is that when django loads it tries to read the secret key from the settings file, but if no such file is loaded it will just fail with the SECRET_KEY must not be empty error.Serafeim
@Serafeim When I run python manage.py, it works; the error only occurs when I run heroku run manage.py, so it must be something about my hekoru setup, but I just can't figure it out.Yuan Tian

2 Answers

2
votes

I faced this issues a while ago. The following command resolved the issue for me

command: heroku config:set SECRET_KEY="your_secret_key"

0
votes

Might be a stupid question, but are you running heroku run python manage.py syncdb?

If so, I am very confident, that your settings module is not loaded.

This could potentially have one of the following reasons:

  • Your settings module is not where Django expects it
    (but you are saying manage.py can find it)
  • You have another config, that overshadows your config settings
    (for example a local.py settings module for development settings)

Did you do any modification to manage.py or wsgi.py?

I edit this answer if I can think of more.