📝 Django

Deploying Django to Railway

P
Author
Pyland
📅
Published
30.06.2026
⏱️
Reading time
1 min
👁️
Views
89
🌳
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

📝

Event Loop in Python: How asyncio Achieves "Paral…

Event loop is the heart of asyncio. It doesn't run code in parallel across multiple...

📅 30.06.2026 👁️ 129
📝

pytest-django: Testing Django

Охватываемые темы: Installation, @pytest.mark.djangodb, Fixtures, Testing views.

📅 30.06.2026 👁️ 138
📝

run_in_executor and anyio: Sync Libraries in Asyn…

Sometimes you need to call a synchronous library from async code without blocking the event...

📅 30.06.2026 👁️ 107

Did you like the article?

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