I am using python-decouple 3.4 for setting up environment variables for my django application. My .env
file is in the same directory as that of manage.py
. Except for SECRET_KEY (in settings.py), loading other environment variables in either settings.py
or views.py
directly fails stating that they have not been defined. The other environment variables which give error will be used in views.py
.
Here is my .env
file:-
SECRET_KEY=<django_app_secret_key>
file_path=<path_to_the file>
If I try to define them in settings.py
like:-
from decouple import config
FILE_PATH = config('file_path')
and then use them in views.py
,
from django.conf.settings import FILE_PATH
print(FILE_PATH)
then also I get the same error. How can I define environment variable for my views.py
specifically?
[Edit: This is the error which I get:-
raise UndefinedValueError('{} not found. Declare it as envvar or define a default value.'.format(option))
decouple.UndefinedValueError: file_path not found. Declare it as envvar or define a default value.
whether I used this
from decouple import config
FILE_PATH = config('file_path')
in settings.py
directly or views.py
directly or first in settings.py
and then in views.py
like the example shown above]
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.