I want to put my django database settings into a .env file for production.
While I successfully wrote SECRET KEY
and DEBUG
in my .env file, when I try to do the same thing for my database settings, I get an error in the webapp.
Here is the way I went about it:
SECRET_KEY = config('SECRET_KEY')
DEBUG = config('DEBUG', default=False, cast=bool)
DATABASE_ENGINE ='django_tenants.postgresql_backend'
DATABASE_NAME = config('DATABASE_NAME')
DATABASE_USER = config('DATABASE_USER')
DATABASE_PASSWORD = config('DATABASE_PASSWORD')
DATABASE_HOST = config('DATABASE_HOST', cast=db_url)
DATABASE_PORT = config('PORT', cast=int)
and in my .env file
SECRET_KEY = supersecretkey
DEBUG = False
DATABASE_NAME = mydb
DATABASE_USER = me
DATABASE_PASSWORD = greatpassword
DATABASE_HOST =urlurl
DATABASE_PORT = 5432
Previously I had my db settings written as such and everything was working great:
DATABASES = {
'default': {
'ENGINE': 'django_tenants.postgresql_backend',
'NAME': 'mydb',
'USER': 'me',
'PASSWORD' : 'greatpassword',
'HOST': 'urlurl',
'PORT': '5432',
}
}
UPDATE: I have tried the following structure in settings.py thanks to the comments, but still get the same error:
DATABASES = {
'default': {
'ENGINE': 'django_tenants.postgresql_backend',
'NAME': config('DATABASE_NAME'),
'USER': config('DATABASE_USER'),
'PASSWORD' : config('DATABASE_PASSWORD'),
'HOST': 'config('DATABASE_HOST', cast=db_url),
'PORT': config('DATABASE_PORT'),
}
}
Django's log is outputing this but I can't really see how it relates to the issue at hand:
Traceback (most recent call last):
File "/home/ubuntu/exo/lib/python3.6/site-packages/django/template/base.py", line 850, in _resolve_lookup
(bit, current)) # missing attribute
django.template.base.VariableDoesNotExist: Failed lookup for key [redirect_to] in [{'True': True, 'False': False, 'None': None}, {'csrf_token': <SimpleLazyObject: '3UKtg6BF5E1qXSTE8nlEevd4e4jVZMcgpdx5W0NZIrB18yuOoCJqnW7xxc7WBfyG'>, 'request': <WSGIRequest: GET '/'>, 'user': <SimpleLazyObject: <function AuthenticationMiddleware.process_request.<locals>.<lambda> at 0x7fa6a25$
question from:
https://stackoverflow.com/questions/65869390/django-db-settings-into-a-env-file-with-decouple