[英]How do you set Django environment variables with venv on linux?
I've tried using Python's os, manually typing the variables into the terminal, and using a.env file to set my environment variables but always get我尝试过使用 Python 的操作系统,在终端中手动输入变量,并使用 a.env 文件设置我的环境变量,但总是得到
raise ImproperlyConfigured("The SECRET_KEY setting must not be empty.")
django.core.exceptions.ImproperlyConfigured: The SECRET_KEY setting must not be empty.
# env.py
import os
os.environ['SECRET_KEY'] = '<my secret key>'
export SECRET_KEY=<my secret key>
SECRET_KEY = os.getenv("SECRET_KEY")
Running my python script will not make the environment variables persist.运行我的 python 脚本不会使环境变量持续存在。
I have double checked the correct venv activated.我已经仔细检查了正确的 venv 激活。
What am I doing wrong!我究竟做错了什么! Thanks谢谢
Django itself does not read .env
file. Django 本身不读取.env
文件。 The best way, to enable it is to use django-environ package启用它的最佳方法是使用django-environ package
Modify you settings.py修改你的settings.py
import environ
env = environ.Env(
# set casting, default value
DEBUG=(bool, False)
)
And.env file和.env文件
SECRET_KEY=your-secret-key
try if in settings.py exist:试试 settings.py 中是否存在:
SECRET_KEY = '<my secret key>'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.