Cannot Get Environment Variables in Django Settings File

Cannot get environment variables in Django settings file

I've manage to solve my problem by using this solution:

http://drumcoder.co.uk/blog/2010/nov/12/apache-environment-variables-and-mod_wsgi/

Unable to get environment variables inside django settings?

In many shells, setting a variable only does so for the shell process itself. If you want to turn a shell variable into an environment variable then you need to export the variable, usually with the export command.

Django can't see environment variables?

Odds are you forgot to export it. You can check if that's the problem with:

export -p | fgrep RETR_DB_NAME

which will output nothing if you forgot to export, and export can be used to make it exported (whether or not it was already exported, it's not an error to export twice) with:

export RETR_DB_NAME

os.environ in django settings.py cannot get system environment variables with apache and wsgi

You say "The environment variables were set in .bashrc." Presumably you mean your .bashrc. Which is pointless, because Apache is not running as you, it is running as the Apache user.

As explained in the blog post referenced in the very question you link to, you need to set the environment variables in the Apache configuration file itself via the SetEnv directive.

Couldn't able to import environment variable for django settings.py for sending email in linux ubuntu system

Firstly, don't use os.environ.get('...') - it silently fails when the environment variable is missing. Use os.environ['...'] instead.

EMAIL_HOST_USER = os.environ['EMAIL_HOST_USER']
print('variable :',os.environ['EMAIL_HOST_USER'])
EMAIL_HOST_PASSWORD = os.environ['EMAIL_HOST_PASSWORD']

Next, the .bashrc or .bash_profile will only work if you are running Django from a shell that has sourced those files. Remove the import os, it is not Python.

Next, you still need the export in your shell if you set the variables before running Django.

export EMAIL_HOST_USER=name@gmail.com
export EMAIL_HOST_PASSWORD=123456789

If you want to set the environment variables in Python, then treat os.environ as a dict instead of trying to call .set(...).

import os
os.environ['EMAIL_HOST_USER'] = 'name@gmail.com'
os.environ['EMAIL_HOST_PASSWORD'] = 12345678

Finally, even if this works on your local box, it might stop working when you deploy on a server with a different IP address. Every week I see questions on Stack Overflow where users are struggling to send emails from Django using gmail. I usually suggest that they think about using a different email provider.

.env variables not visible inside settings.py file of a Django project run with Docker

i would recommend you django-environ, check documentation at https://django-environ.readthedocs.io/en/latest/

once installed via pip install django-environ, create .env file in the root of your project:

in /.env

SECRET_KEY="#b)oj5(wpt!1f1e8+%1sa4gcp*4b5#k^w=15gg80=_=3oavf3w"
DEBUG=on

and then in settings.py, you can use your predefined environment variables like

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)

[..]

Django environment variables aren't working

You are not running django from the same shell that sets the env variable (your user shell runs .bashrc but apache does not)

Try adding the environment variable under /etc/apache2/envvars and restart apache service.

Then when running django it should see the env-var setting.

Another thing, it is important not to have spaces when setting an environment variable, it should be:

export DB_NAME=Candlelight

How to use environment variables in views.py in Django?

In the .env file, the values assigned to variables were not enclosed in qoutes and that was why it was giving the error that it was unable to find file_path variable.

The .env file should be like this:-

SECRET_KEY='<django_app_secret_key>'
file_path='<path_to_the file>'

Anyways thanks to @Iain Shelvington and @Prakash S for your help.



Related Topics



Leave a reply



Submit