Django REST Framework on Railway: Environment Variables Not Being Recognized[solved]
cardlo
HOBBYOP

a year ago

I'm facing an issue with the environment variables in my Django REST Framework (DRF) project deployed on Railway. I've correctly defined the variables in the Railway dashboard, but it seems my application is not recognizing them. Even the variable pointing to the PostgreSQL database provided by Railway is not being detected. When checking through the Railway CLI, my configured variables do not appear. Can someone help me with this issue? Thanks in advance.

These are the variables I have configured in Railway, and the names are correctly set:

DATABASE_URL="${{Postgres.DATABASE_URL}}" SECRET_KEY="django-insecure-xxxx"

I call my variables in my settings.py file using:

os.getenv('SECRET_KEY') os.getenv('DATABASE_URL')

Any help would be greatly appreciated!

Solved

8 Replies

chandrika
EMPLOYEE

a year ago

Hi there, could you try checking out our guide on deploying Django and see if that helps? https://docs.railway.com/guides/django

If so, please do share your solution here so others in the future are able to view and get help from it too!


Status changed to Awaiting User Response Railway about 1 year ago


djnxe
TRIAL

a year ago

You might have simplified the code for your post, but just in case: Django itself doesn't have a DATABASE_URL setting. It's common to use another library for that, but if not you have to fill in each part separately, like:

DATABASES = {
    "default": {
        "ENGINE": "django.db.backends.postgresql",
        "NAME": os.getenv('PGDATABASE'),
        "USER": os.getenv('PGUSER'),
        "PASSWORD": os.getenv('PGPASSWORD'),
        "HOST": os.getenv('PGHOST'),
        "PORT": os.getenv('PGPORT'),
    }
}

Status changed to Awaiting Railway Response Railway about 1 year ago


djnxe

You might have simplified the code for your post, but just in case: Django itself doesn't have a DATABASE_URL setting. It's common to use another library for that, but if not you have to fill in each part separately, like:DATABASES = { "default": { "ENGINE": "django.db.backends.postgresql", "NAME": os.getenv('PGDATABASE'), "USER": os.getenv('PGUSER'), "PASSWORD": os.getenv('PGPASSWORD'), "HOST": os.getenv('PGHOST'), "PORT": os.getenv('PGPORT'), } }

chandrika
EMPLOYEE

a year ago

Thank you djnxe!

Cardlo, could you please see if the above helps ^?


Status changed to Awaiting User Response Railway about 1 year ago


djnxe

You might have simplified the code for your post, but just in case: Django itself doesn't have a DATABASE_URL setting. It's common to use another library for that, but if not you have to fill in each part separately, like:DATABASES = { "default": { "ENGINE": "django.db.backends.postgresql", "NAME": os.getenv('PGDATABASE'), "USER": os.getenv('PGUSER'), "PASSWORD": os.getenv('PGPASSWORD'), "HOST": os.getenv('PGHOST'), "PORT": os.getenv('PGPORT'), } }

cardlo
HOBBYOP

a year ago

I use dj_database_url. As I mentioned, my problem is not with the database connection. My problem is that railway does not take the environment variables.


Status changed to Awaiting Railway Response Railway about 1 year ago


chandrika

Hi there, could you try checking out our guide on deploying Django and see if that helps? https://docs.railway.com/guides/djangoIf so, please do share your solution here so others in the future are able to view and get help from it too!

cardlo
HOBBYOP

a year ago

The error:

My variables:

I try with:

SECRECT_KEY = os.environ.get('SECRET_KEY')
SECRECT_KEY = os.environ['SECRET_KEY']
SECRECT_KEY = os.environ.setdefault['SECRET_KEY']

chandrika

Thank you djnxe! Cardlo, could you please see if the above helps ^?

cardlo
HOBBYOP

a year ago

I still have the error


cardlo
HOBBYOP

a year ago

I have already found my error: the first thing is that you have to write: os.environ.get('SECRET_KEY') , it didn't work for me writing: os.getenv('SECRET_KEY')

I also had an error that only happened in SECRET_KEY because I had written the value wrong. Thks! It was helpful to consult: https://docs.railway.com/guides/django


a year ago

Hello,

You have yet to apply the changes after you set the variables -

Attachments


Status changed to Awaiting User Response Railway about 1 year ago


Railway
BOT

7 months ago

This thread has been marked as solved automatically due to a lack of recent activity. Please re-open this thread or create a new one if you require further assistance. Thank you!

Status changed to Solved Railway 7 months ago


Loading...