简体   繁体   中英

set command in windows not working for django SECRET_KEY | django.core.exceptions.ImproperlyConfigured: Set the SECRET_KEY environment variable

I set the SECRET_KEY with set command in the terminal, but nothing works

set SECRET_KEY='9l=jjp#g0-mbdfsntqww91s9b^a!kj44ljl4f5h!+uoft'

settings.py:

from pathlib import Path
import environ

# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent

env = environ.Env(
DEBUG=(bool, False)
)

READ_DOT_ENV_FILE = env.bool('READ_DOT_ENV_FILE', default=False)
if READ_DOT_ENV_FILE:
    environ.Env.read_env()

DEBUG = env('DEBUG')

SECRET_KEY = env('SECRET_KEY')

ALLOWED_HOSTS = []

.env:

SECRET_KEY='9l=jjp#g0-mbdfsntqww91s9b^a!kj44ljl4f5h!+uoft'
DEBUG=True

So as you can read, the READ_DOT_ENV_FILE should be True to let django read the system variables from.env file, otherwise, it'll read the variables from the terminal (session) by defining them in it using the set command.

In this case:

set DEBUG=True

set SECRET_KEY='9l=jjp#g0-mbdfsntqww91s9b^a!kj44ljl4f5h!+uoft'

Done. I found the solution. Instead of using set , you have to use:

[System.Environment]::SetEnvironmentVariable('value','key')

I tried that and It works with setting the SECRET_KEY & DEBUG variables.

Closed.

I had this exact issue and I used $env:DEBUG="True". $env:SECRET_KEY="paste_your_secret_key_here".

It's surely gonna work

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM