Unable to set an environment variable for SECRET_KEY - AWS Lightsail

Keywords: Django - AWS - Technical issue - Other
Description:
I am unable to set an environment variable for SECRET_KEY. The website loads fine when I leave the variable as plaintext in my settings.py file, but when I try to load the value of SECRET_KEY from my environment file, it is unable to be read, and my website loads this message.

"Internal Server Error

The server encountered an internal error or misconfiguration and was unable to complete your request.

Please contact the server administrator at you@example.com to inform them of the time this error occurred, and the actions you performed just before this error.

More information about this error may be available in the server error log."

I would like to fix this problem and i can also attach my settings.py as well as my directory.

settings.py:

from pathlib import Path
import os
#import environ
#from configurations import Configuration

from dotenv import load_dotenv
load_dotenv()
# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent
SECRET_KEY = os.getenv('SECRET_KEY')

.env file:

SECRET_KEY = *

Hi @uc6991,

Thanks for using Bitnami. Can you check the load_dotenv() function is working properly? Can you check the .env file permissions are right? Apparently, this seems to be more specific to the Django code than to the Bitnami installation.

We have a Support Tool that will gather relevant information for us to analyze your configuration and logs. Could you please execute it on the machine where the stack is running by following the steps described in the guide below?

Please note that you need to paste the code ID that is shown at the end.

i found a solution that worked for me.

after these initial statements:

from pathlib import Path
import os
BASE_DIR = Path(file).resolve().parent.parent

I pointed my load_dotenv function to the folder that held my .env

from dotenv import load_dotenv
load_dotenv(os.path.join(BASE_DIR,“PROJECT”,".env"))

here was the link that helped me

https://stackoverflow.com/a/57479963

Hi @uc6991,

Thanks for the info. I’m glad you fixed your issue! We will close this thread as solved. Please do not hesitate to open a new one with any other questions you may have.