VS 2019 django The SECRET_KEY setting must not be empty

Rick 6 Reputation points
2020-12-31T18:19:16.107+00:00

How can I set the SECRET_KEY in a django project? I have created a new django project from existing code and in my settings.py file I have SECRET_KEY = '4blyti@u4yj$skr7e&g63f*l%#jl(xq&od-2y&qzo^rrs!_p#7'. When I try to make migrations I get django.core.exceptions.ImproperlyConfigured: The SECRET_KEY setting must not be empty. I also have os.environ.setdefault("DJANGO_SETTINGS_MODULE", "tutorial.settings.prod") in my wsgi.py file.

Developer technologies | Visual Studio | Other
{count} votes

2 answers

Sort by: Most helpful
  1. Rick 6 Reputation points
    2021-01-11T02:33:13.06+00:00

    TianyuSun,
    I did try manually setting DJANGO_SETTINGS_MODULE and SECRET_KEY manually as noted above but it still had the same error. I have solved the issue by stepping into the code in debug I found that the init.py file was executing then it was exiting the program without going to the settings.py file. So in the init.py file I added
    from .base import *

    env_name = os.getenv('ENV_NAME', 'local')

    if env_name == 'prod':
    from .prod import *
    elif env_name == 'dev':
    from .dev import *
    else:
    from .local import *

    This did resolve the issue.

    1 person found this answer helpful.
    0 comments No comments

  2. Tianyu Sun-MSFT 34,441 Reputation points Microsoft External Staff
    2021-01-05T07:17:01.217+00:00

    Hi @Rick ,

    Thank you for your feedback.

    Looks like the issue is caused by this tutorial project(some settings…), if you try to run following command in the terminal will this issue disappear?

    set DJANGO_SETTINGS_MODULE=tutorial.settings.dev
    set SECRET_KEY='*'

    (remember to replace the * with your secret key in the settings.py file)

    If this issue still persists, I suggest you can directly ask author for this issue and check the related issues reported in GitHub > Issues of this Django tutorial.

    Sincerely,
    Tianyu

    • If the answer is helpful, please click "Accept Answer" and upvote it.
      Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.