Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
306 views
in Technique[技术] by (71.8m points)

How do you get the settings.py file of a django project to read from .env?

I am trying to configure email environment settings in my django project. I am using django-environ package. I have pasted this code:

EMAIL_CONFIG = env.email_url('EMAIL_URL',default='smtp://user@:password@localhost:25')
vars().update(EMAIL_CONFIG)

in my .env file. But when i run the server am getting an error saying:

"django.core.exceptions.ImproperlyConfigured: Set the EMAIL_CONFIG environment variable"

what am getting wrong?

question from:https://stackoverflow.com/questions/65908171/how-do-you-get-the-settings-py-file-of-a-django-project-to-read-from-env

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I don't think that this settings should part of your .env. This file should look like this

DEBUG=on
SECRET_KEY=your-secret-key
DATABASE_URL=psql://user:[email protected]:8458/database
SQLITE_URL=sqlite:///my-local-sqlite.db
CACHE_URL=memcache://127.0.0.1:11211,127.0.0.1:11212,127.0.0.1:11213
REDIS_URL=rediscache://127.0.0.1:6379/1?client_class=django_redis.client.DefaultClient&password=ungithubbed-secret

I guess your settings (taken from the docs) should be used inside settings.py or in a custom email_config

EMAIL_CONFIG = env.email_url('EMAIL_URL',default='smtp://user@:password@localhost:25')
vars().update(EMAIL_CONFIG)

Check the installation and usage docs from django-environ.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...