Deploy a Django App
Django is a powerful Python web framework that simplifies web development by providing ready-to-use tools for rapid development and clean design.
It’s free, open-source, and comes with a range of features to streamline tasks like authentication, routing, and database management, so developers can focus on building their applications without handling everything from scratch.
Create a Django App
Note: If you already have a Django app locally or on GitHub, you can skip this step and go straight to the Deploy Django App on Railway.
To create a new Django app, ensure that you have Python and Django installed on your machine.
Follow the steps below to set up the project in a directory.
Create a virtual environment
python -m venv env
Activate the virtual environment
source env/bin/activate
Note: For windows developers, run it as env\Scripts\activate
in your terminal.
Install Django
python -m pip install django
Once everything is set up, run the following command in your terminal to provision a new Django project:
django-admin startproject liftoff
This command will create a new Django project named liftoff
.
Next, cd
into the directory and run python manage.py runserver
to start your project.
Open your browser and go to http://127.0.0.1:8000
to see the project. You'll see the Django welcome page with a "The install worked successfully! Congratulations!" paragraph.
Note: You'll see a red notice about unapplied migration(s). You can ignore them for now. We'll run them when we deploy the project.
Now that your app is running locally, let’s move on to make some changes and install some dependencies before deployment.
Configure Database, Static Files & Dependencies
- Install the following packages within the
liftoff
directory, where you can see themanage.py
file.
python -m pip install gunicorn whitenoise psycopg[binary,pool]
whitenoise is a Python package for serving static files directly from your web app. It serves compressed content and sets far-future cache headers on content that won't change.
gunicorn is a production ready web server.
pyscog is python package that allows Django work with Postgresql.
- Import the
os
module:
Open the liftoff/settings.py
file located in the inner liftoff
directory (the one containing the main project settings).
At the top of the file, add the following line to import the os
module, placing it just before the Path
import:
import os
from pathlib import Path
- Configure the database and run migrations:
A fresh Django project uses SQLite by default, but we need to switch to PostgreSQL.
Create a database named liftoff_dev
in your local Postgres instance.
Open the liftoff/settings.py
file. In the Database section, replace the existing configuration with:
# Database
# https://docs.djangoproject.com/en/5.1/ref/settings/#databases
# Set default values for the environment variables if they’re not already set
os.environ.setdefault("PGDATABASE", "liftoff_dev")
os.environ.setdefault("PGUSER", "username")
os.environ.setdefault("PGPASSWORD", "")
os.environ.setdefault("PGHOST", "localhost")
os.environ.setdefault("PGPORT", "5432")
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': os.environ["PGDATABASE"],
'USER': os.environ["PGUSER"],
'PASSWORD': os.environ["PGPASSWORD"],
'HOST': os.environ["PGHOST"],
'PORT': os.environ["PGPORT"],
}
}
Replace the values of PGUSER
, PGPASSWORD
with your local credentials.
Run python manage.py migrate
in your terminal to apply the database migrations. Once it completes successfully, check your database. You should see the auth and other Django tables created.
- Static files configuration:
We'll configure Django to serve static files using WhiteNoise.
Open liftoff/settings.py
and configure the static files settings:
STATIC_URL = 'static/'
STATIC_ROOT = os.path.join(BASE_DIR, "staticfiles")
STATICFILES_DIRS = [os.path.join(BASE_DIR, "static")]
Add the WhiteNoise middleware in the MIDDLEWARE section, just below the security middleware:
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'whitenoise.middleware.WhiteNoiseMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]
- Update
ALLOWED_HOSTS
settings:
ALLOWED_HOSTS = ["*"]
This setting represents the list of all the host/domain names our Django project can serve.
- Create a static folder:
Inside your liftoff
directory, create a static folder where all static assets will reside.
- Create a
requirements.txt
file:
To track all the dependencies for deployment, create a requirements.txt
file:
pip freeze > requirements.txt
Note: It's only safe to run the command above in a virtual environment, else it will freeze all python packages installed on your system.
With these changes, your Django app is now ready to be deployed to Railway!
Deploy Django App on Railway
Railway offers multiple ways to deploy your Django app, depending on your setup and preference. Choose any of the following methods:
One-Click Deploy From a Template
If you’re looking for the fastest way to get started, the one-click deploy option is ideal. It sets up a Django app along with a Postgres database.
Click the button below to begin:
After deploying, we recommend that you eject from the template to create a copy of the repository under your own GitHub account. This will give you full control over the source code and project.
Deploy From the CLI
To deploy the Django app using the Railway CLI, please follow the steps:
- Install the Railway CLI:
- Install the CLI and authenticate it using your Railway account.
- Initialize a Railway Project:
- Run the command below in your Django app directory.
railway init
- Follow the prompts to name your project.
- After the project is created, click the provided link to view it in your browser.
- Run the command below in your Django app directory.
- Deploy the Application:
- Use the command below to deploy your app:
railway up
- This command will scan, compress and upload your app's files to Railway. You’ll see real-time deployment logs in your terminal.
- Use the command below to deploy your app:
- Add a Database Service:
- Run
railway add
. - Select
PostgreSQL
by pressing space and hit Enter to add it to your project. - A database service will be added to your Railway project.
- Run
- Configure Environment Variables:
- Go to your app service Variables section and add the following:
PGDATABASE
: Set the value to${{Postgres.PGDATABASE}}
(this references the Postgres database name). Learn more about referencing service variables.PGUSER
: Set the value to${{Postgres.PGUSER}}
PGPASSWORD
: Set the value to${{Postgres.PGPASSWORD}}
PGHOST
: Set the value to${{Postgres.PGHOST}}
PGPORT
: Set the value to${{Postgres.PGPORT}}
- Use the Raw Editor to add any other required environment variables in one go.
- Go to your app service Variables section and add the following:
- Redeploy the App Service:
- Click Deploy on the app service on the Railway dashboard to apply your changes.
- Verify the Deployment:
- Once the deployment completes, go to View logs to check if the server is running successfully.
- Set Up a Public URL:
- Navigate to the Networking section under the Settings tab of your new service.
- Click Generate Domain to create a public URL for your app.
Deploy From a GitHub Repo
To deploy the Django app to Railway, start by pushing the app to a GitHub repo. Once that’s set up, follow the steps below to complete the deployment process.
-
Create a New Project on Railway:
- Go to Railway to create a new project.
-
Deploy from GitHub:
- Select Deploy from GitHub repo and choose your repository.
- If your Railway account isn’t linked to GitHub yet, you’ll be prompted to do so.
- Select Deploy from GitHub repo and choose your repository.
-
Add Environment Variables:
- Click Add Variables and configure all the necessary environment variables for your app.
PGDATABASE
: Set the value to${{Postgres.PGDATABASE}}
(this references the Postgres database name). Learn more about referencing service variables.PGUSER
: Set the value to${{Postgres.PGUSER}}
PGPASSWORD
: Set the value to${{Postgres.PGPASSWORD}}
PGHOST
: Set the value to${{Postgres.PGHOST}}
PGPORT
: Set the value to${{Postgres.PGPORT}}
Note: We don't have the Postgres Database service yet. We'll add that soon.
- Click Add Variables and configure all the necessary environment variables for your app.
-
Add a Database Service:
- Right-click on the Railway project canvas or click the Create button.
- Select Database.
- Select Add PostgreSQL from the available databases.
- This will create and deploy a new Postgres database service for your project.
-
Deploy the App:
- Click Deploy to start the deployment process and apply all changes.
- Once deployed, a Railway service will be created for your app, but it won’t be publicly accessible by default.
-
Verify the Deployment:
- Once the deployment completes, go to View logs to check if the server is running successfully.
Note: During the deployment process, Railway will automatically detect that it’s a Django app.
-
Set Up a Public URL:
- Navigate to the Networking section under the Settings tab of your new service.
- Click Generate Domain to create a public URL for your app.
This guide covers the main deployment options on Railway. Choose the approach that suits your setup, and start deploying your Django apps effortlessly!
Note: The next step shows how to configure and run your Django app along with Celery and Celery beat.
Set Up Database, Migrations, Celery Beat and Celery
This setup deploys your Django app on Railway, ensuring that your database, scheduled tasks (crons)--Celery Beat, and queue workers (Celery) are all fully operational.
The deployment structure follows a "majestic monolith" architecture, where the entire Django app is managed as a single codebase but split into four separate services on Railway:
- App Service: Handles HTTP requests and user interactions.
- Cron Service: Manages scheduled tasks (e.g., sending emails or running reports) using Celery Beat.
- Worker Service: Processes background jobs from the queue using Celery.
- Database Service: Stores and retrieves your application's data.
My Monolith Django app
Note: This guide follows the assumption that you have installed Celery and Celery Beat in your app, the broker uses Redis and you already have a Postgres database service provisioned for your app as shown earlier.
Please follow these steps to get it setup on Railway:
- Create a Redis Database service on the Project Canvas by clicking the Create button. Then select Database and choose Add Redis.
- Click on Deploy.
- Create a new service on the Project Canvas by clicking the Create button. Then select Empty service.
- Name the service App Service, and click on Settings to configure it.
- Note: If you followed the guide from the beginning, simply rename the existing service to App Service.
- Connect your GitHub repo to the Source Repo in the Source section.
- Go to the top of the service and click on Variables.
- Add all the necessary environment variables required for the Django app especially the ones listed below.
REDIS_URL
: Set the value to${{Postgres.REDIS_URL}}
PGUSER
: Set the value to${{Postgres.PGUSER}}
PGPASSWORD
: Set the value to${{Postgres.PGPASSWORD}}
PGHOST
: Set the value to${{Postgres.PGHOST}}
PGPORT
: Set the value to${{Postgres.PGPORT}}
PGDATABASE
: Set the value to${{Postgres.PGDATABASE}}
(this references the Postgres database name). Learn more about referencing service variables.
- Click Deploy.
- Name the service App Service, and click on Settings to configure it.
- Create a new service on the Project Canvas by clicking the Create button. Then select Empty service.
- Name the service Cron Service, and click on Settings.
- Connect your GitHub repo to the Source Repo in the Source section.
- Add
celery -A liftoff beat -l info --concurrency=3
to the Custom Start Command in the Deploy section.- Note:
liftoff
is the name of the app. You can find the app name in your Django project’s main folder, typically in the directory containingsettings.py
. - The
--concurrency=3
option here means it can process up to 3 tasks in parallel. You can adjust the concurrency level based on your system resources. The higher the level, the more memory and resources it consumes.
- Note:
- Head back to the top of the service and click on Variables.
- Add all the necessary environment variables especially those highlighted already in step 2.
- Click Deploy.
- Create a new service again on the Project Canvas.
- Name the service Worker Service, and click on Settings.
- Connect your GitHub repo to the Source Repo in the Source section.
- Add
celery -A liftoff worker -l info --concurrency=3
to the Custom Start Command in the Deploy section.- Note:
liftoff
is the name of the app. You can find the app name in your Django project’s main folder, typically in the directory containingsettings.py
. - The
--concurrency=3
option here means it can process up to 3 tasks in parallel. You can adjust the concurrency level based on your system resources. The higher the level, the more memory and resources it consumes.
- Note:
- Head back to the top of the service and click on Variables.
- Add all the necessary environment variables especially those highlighted already in step 2.
- Click Deploy.
At this point, you should have all services deployed and connected to the Postgres and Redis Database service:
- Cron Service: This service should run Celery Beat Scheduler to manage scheduled tasks.
- Worker Service: This service should be running Celery and ready to process jobs from the queue.
- App Service: This service should be running and is the only one that should have a public domain, allowing users to access your application.
Note: There is a community template available that demonstrates this deployment approach. You can easily deploy this template and then connect it to your own GitHub repository for your application.
Next Steps
Explore these resources to learn how you can maximize your experience with Railway:
Edit this file on GitHub