django 启动期间验证设置的位置

jpfvwuh4  于 2022-11-26  发布在  Go
关注(0)|答案(1)|浏览(120)

在settings.py执行之后,我需要检查以确保从.env变量中找到了所有设置,如果没有找到,则输出错误settings.py?manage.py?

igetnqfo

igetnqfo1#

'我建议你django-environ,检查https://django-environ.readthedocs.io/en/latest/上的文档
一旦通过pip install django-environ,create .env输入,就可以进入项目的基础:
在/.env中
之后在www.example.com中settings.py,您可以使用预定义的环境变量,如:

import os

# django-environ
# https://django-environ.readthedocs.io/en/latest/
import environ

from django.utils.translation import ugettext_lazy as _

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

# Load and read .env file
# OS environment variables take precedence over variables from .env
env = environ.Env()
env.read_env(os.path.join(BASE_DIR, '.env'))

# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/2.2/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = env('SECRET_KEY')

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = env.bool('DEBUG', False)```

相关问题