๐Ÿ“ Django

Deploying Django to Railway

P
Author
PyLand Team
๐Ÿ“…
Published
30.06.2026
โฑ๏ธ
Reading time
1 min
๐Ÿ‘๏ธ
Views
317
๐ŸŒณ
Level
Advanced

Railway is a cloud platform with a free tier. Deploy from GitHub in minutes.

Preparing the project

pip install gunicorn whitenoise dj-database-url
pip freeze > requirements.txt
# settings.py
import os
import dj_database_url

DEBUG = os.environ.get('DEBUG', 'False') == 'True'
SECRET_KEY = os.environ.get('SECRET_KEY')
ALLOWED_HOSTS = os.environ.get('ALLOWED_HOSTS', '').split(',')

# Database from environment variable
DATABASES = {
    'default': dj_database_url.config(
        default=os.environ.get('DATABASE_URL'),
        conn_max_age=600,
    )
}

# Static files via whitenoise
MIDDLEWARE = [
    'whitenoise.middleware.WhiteNoiseMiddleware',
    ...
]
STATIC_ROOT = BASE_DIR / 'staticfiles'
STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'

Procfile

Create a Procfile in the project root:

web: gunicorn mysite.wsgi --workers 2 --bind 0.0.0.0:$PORT
release: python manage.py migrate

.gitignore

.env
*.pyc
__pycache__/
db.sqlite3
staticfiles/
media/

Deploying to Railway

  1. Push the project to GitHub
  2. Go to railway.app โ†’ New Project โ†’ Deploy from GitHub
  3. Select your repository
  4. Add PostgreSQL: New โ†’ Database โ†’ PostgreSQL
  5. In the service settings, add the following variables:
    - SECRET_KEY โ€” generate one: python -c "from django.core.management.utils import get_random_secret_key; print(get_random_secret_key())"
    - DEBUG = False
    - ALLOWED_HOSTS = your-app.railway.app
    - DATABASE_URL โ€” pulled in automatically from the PostgreSQL service

Post-deploy commands

# In Railway โ†’ Service โ†’ Shell (or via CLI)
python manage.py migrate
python manage.py collectstatic --no-input
python manage.py createsuperuser

Your reaction to the article

๐Ÿ’ฌ Comments (0)

๐Ÿ” Sign in to leave a comment
๐Ÿšช Login
๐Ÿ’ญ

No comments yet

Be the first to share your opinion about this article!

๐Ÿ”— Similar

Similar articles

Continue learning with these materials

๐Ÿ“

settings.py: Django Configuration

settings.py is the central configuration file for a Django project.

๐Ÿ“… 30.06.2026 ๐Ÿ‘๏ธ 315
๐Ÿ“

CSRF Protection in Django

CSRF (Cross-Site Request Forgery) is an attack in which an adversary tricks the user's browser...

๐Ÿ“… 30.06.2026 ๐Ÿ‘๏ธ 294
๐Ÿ“

Django Migrations

A migration is a file describing changes to the database schema. Django tracks the state...

๐Ÿ“… 30.06.2026 ๐Ÿ‘๏ธ 286

Did you like the article?

Subscribe to our updates and be the first to receive new articles. Grow with PyLand!