In this tutorial, I'll walk you through the steps to deploy your Django project to Railway. Railway is a platform for deploying and managing web applications.
Before you begin, make sure you have the following prerequisites:
- A Django project that you want to deploy.
- Git installed on your local machine.
- A Railway account. Sign up for Railway if you don't have one.
Follow the steps below to prepare your Django project for deployment:
-
In your settings.py file, update the following variables as pictured:
If you have a front end deployed that will access this Django deployment, add your front end url to the CSRF_TRUSTED_ORIGINS array:
-
CSRF_TRUSTED_ORIGINS = ["http://yourfrontend.com"]
-
-
This step is only necessary if you are serving any static files from a framework, such as Django REST Framework. If you do not have static files to serve and are only deploying for REST API access, you can skip this step.
-
In your project's root directory, run the following command: pipenv install whitenoise
-
In your settings.py file, add the following to the MIDDLEWARE section: 'whitenoise.middleware.WhiteNoiseMiddleware'
-
In your settings.py file, add the following below the existing STATIC_URL line:
- STATIC_ROOT = os.path.join(BASE_DIR, 'static')
- STATICFILES_STORAGE = 'whitenoise.storage.StaticFilesStorage'
At the top of the settings.py file, insert: import os
- In your project's root directory, run the following command: python3 manage.py collectstatic
-
Congratulations! Your Django project is now ready for deployment. Follow the steps below to deploy your project to Railway:
Visit Railway and log in to your account.
If you haven't done this before, you'll need to initialize a new Railway project for your Django backend. Click on "New Project" and select your Git repository where your Django project is located.
Once your project is initialized, you'll need to configure the deployment settings. Railway will automatically detect that you have a Python project and suggest the appropriate settings. Verify that the detected settings are correct and proceed.
Click on the "Deploy" button, and Railway will begin the deployment process. You can monitor the progress in the Railway dashboard.
After the deployment is successful, Railway will provide you with a URL where your Django app is accessible. Click on the link, and your Django project is now live!