djangoazure-web-app-servicesettingssecret-key

Azure Django Web App: raise KeyError(key) from None KeyError: 'SECRET_KEY'


I'm trying to deploy my django app on azure from the repo on GitHub. In the settings.py file, I dont want to store the secret key directly on GitHub, so I added that on the configuration application setting of the Azure app page. To retrieve it, my settings.py file has

SECRET_KEY = os.environ['SECRET_KEY']

The error I'm getting is: raise KeyError(key) from None KeyError: 'SECRET_KEY'

Am I properly retrieving the secret key?


Solution

  • You are not using the environment variable correctly.

    Firstly, you could set the environment variable in settings.py like below, rather than retrieve it in this file.

    SECRET_KEY = '12345'
    

    You could retrieve the variable in your code like what you displayed:

    secret_key = os.environ['SECRET_KEY']
    

    Considering you don't want to expose your SECRET_KEY value on GitHub, you could set a temp value for invoking, which is required. After deploy, the value on portal App Settings would cover the value.