How to Deploy a Python Django App on Railway

A brief guide to deploying a Python Django app on Railway.

In this post, we'll walk through the steps to deploy a Python Django app on Railway.

What is Django?

Django is an open-source, high-level Python web framework that allows developers to build powerful web applications quickly and easily. Its built-in administration interface allows developers to create, read, update, and delete datain the database without writing any code. Django also offers robust features like authentication, URL routing, template rendering, and more. It is one of the most popular web frameworks around, and benefits from a large and active developer community who contribute to its continual improvement and documentation. Flask, on the other hand, is a micro web framework that is lightweight and flexible, making it better suited for small and medium-sized projects unlike Django, which can easily scale to large projects.

What is Railway?

Railway is a modern app hosting platform that makes it easy to deploy production-ready apps quickly. Railway offers persistent database services for PostgreSQL, MySQL, MongoDB, and Redis, as well as application services with a GitHub repository as the deployment source. For the latter, Railway can automatically determine the application runtime and deploy the service. Railway offers several one-click starters for popular applications, including Django. Since we are just testing the waters, Railway's free tier should be sufficient to host the service.

Source: Railway.app
Source: Railway.app

Deploy a Simple Django App on Railway

Sign up for an account with Railway using GitHub, and click Authorize Railway App when redirected. Review and agree to Railway's Terms of Service and Fair Use Policy if prompted. Launch the Django one-click starter template (or click the button below) to deploy Django instantly on Railway.

Deploy on Railway

You'll be given an opportunity to change the default repository name and set it private, if you'd like. Accept the defaults and click Deploy; the deployment will kick off immediately. The template also deploys a Postgres database instance.

Deploy Django one-click starter template on Railway
Deploy Django one-click starter template on Railway

Once the deployment completes, a simple Django app will be available at a default xxx.up.railway.app domain - launch this URL to access the app. If you are interested in setting up a custom domain, I covered it at length in a previous post - see the final section here.

Sample Django app deployed on Railway
Sample Django app deployed on Railway

If you want to modify the app, here's a few things to understand first:

  • manage.py is Django's command-line utility for administrative tasks.
  • requirements.txt specifies package dependencies.
  • Procfile tells Railway how to run the app.
  • The mysite/ directory is the actual Python package for your project, and the name should be used to import anything inside it e.g. mysite.urls.
  • mysite/__init__.py is an empty file that tells Python this directory should be considered a Python package.
  • mysite/settings.py specifies the configuration for this project.
  • mysite/urls.py specifies the URL declarations for this project, a "table of contents" if you will.
  • mysite/asgi.py is an entry point for ASGI-compatible web servers to serve your project.
  • mysite/wsgi.py is an entry point for WSGI-compatible web servers to serve your project.

In conclusion, deploying a Python Django app on Railway is a straightforward process, made even easier by the one-click template. Go forth and deploy!

Subscribe to alphasec

Don’t miss out on the latest issues. Sign up now to get access to the library of members-only issues.
jamie@example.com
Subscribe