Skip to content
Kuza Ndoto Academy

Deploying Django

IntermediateChapter 6 · Page 22 of 236 min readUpdated 15 Nov 2026

Deploying means putting your Django project on a server connected to the internet so anyone can use it. This page takes the Campus Events project from your laptop to a live address: preparing settings, serving static files, running Gunicorn and publishing on a beginner-friendly host.

The deployment checklist

  1. Secure settings from environment variables: DEBUG=False, a secret SECRET_KEY and the right ALLOWED_HOSTS (see Settings & Security).
  2. An up-to-date requirements.txt.
  3. Static files collected with collectstatic and served efficiently.
  4. A production WSGI server such as Gunicorn instead of runserver.
  5. A database that keeps its data between deploys.
  6. Migrations applied on the server, and an admin user created there.
Browser Host proxy handles HTTPS Gunicorn worker processes Django campus.wsgi PostgreSQL or SQLite staticfiles/ served by WhiteNoise Environment variables: DJANGO_SECRET_KEY, DJANGO_ALLOWED_HOSTS, DATABASE_URL
Figure 1. A typical platform setup, such as Render. Each request travels left to right and the response returns the same way (PythonAnywhere runs your WSGI file with its own server instead of Gunicorn). Settings come from environment variables, and static files are collected once and served efficiently.

Step 1: Install production packages

python -m pip install gunicorn whitenoise
python -m pip freeze > requirements.txt
  • Gunicorn is a production-grade WSGI server. It runs several worker processes and handles many visitors at once.
  • WhiteNoise lets your Django app serve its own static files efficiently, with compression and long-term caching, so you don't need a separate web server just for CSS.

Commit requirements.txt so your host installs exactly the same packages.

Step 2: Configure static files with WhiteNoise

Add WhiteNoise's middleware directly after SecurityMiddleware, and set the static storage:

campus/settings.py

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",
]

STATIC_URL = "static/"
STATIC_ROOT = BASE_DIR / "staticfiles"

STORAGES = {
    "default": {
        "BACKEND": "django.core.files.storage.FileSystemStorage",
    },
    "staticfiles": {
        "BACKEND": "whitenoise.storage.CompressedManifestStaticFilesStorage",
    },
}

Then collect the files (your count and path will differ):

python manage.py collectstatic --no-input
127 static files copied to '/home/amina/campus-events/staticfiles', 361 post-processed.

"Post-processed" files are the hashed and compressed copies WhiteNoise creates for caching. See Static Files for the background.

Step 3: Run with Gunicorn

Gunicorn needs the path to your WSGI application: the application object in campus/wsgi.py.

gunicorn campus.wsgi
[2026-11-15 10:00:00 +0300] [4120] [INFO] Starting gunicorn 23.0.0 [2026-11-15 10:00:00 +0300] [4120] [INFO] Listening at: http://127.0.0.1:8000 (4120) [2026-11-15 10:00:00 +0300] [4120] [INFO] Using worker: sync [2026-11-15 10:00:00 +0300] [4121] [INFO] Booting worker with pid: 4121

Your times, version and process IDs will differ. On a host you'll usually add options such as --bind 0.0.0.0:8000 or --workers 3, depending on its instructions.

Note

Gunicorn runs on Linux and macOS but not on Windows. That's fine: your host runs Linux. To test a production-style server on Windows, use WSL, or try the Waitress server (waitress-serve --listen=127.0.0.1:8000 campus.wsgi:application).

Step 4: Choose a database

SQLite stores everything in one file, db.sqlite3. That's fine on hosts with a permanent disk, such as PythonAnywhere. But many modern platforms, including Render's web services, use an ephemeral filesystem: files written by your app are wiped on every deploy or restart. There, use a managed PostgreSQL database. The dj-database-url package reads its address from a DATABASE_URL environment variable:

python -m pip install dj-database-url "psycopg[binary]"

campus/settings.py

import dj_database_url

DATABASES = {
    "default": dj_database_url.config(
        default=f"sqlite:///{BASE_DIR / 'db.sqlite3'}",
        conn_max_age=600,
    )
}

Locally, with no DATABASE_URL set, you keep using SQLite. On the server, PostgreSQL is used automatically.

Step 5: Publish on a host

Hosting plans and free tiers change often, so check each provider's current plans and documentation. The overall steps look like this.

Option A: Render (deploy from GitHub)

  1. Push your project to a GitHub repository (without .venv, db.sqlite3 or secrets).
  2. If you need PostgreSQL, create a database in Render first and copy its internal connection URL.
  3. Create a new Web Service and connect your repository.
  4. Set the build command and start command:
    # Build command
    pip install -r requirements.txt && python manage.py collectstatic --no-input && python manage.py migrate
    
    # Start command
    gunicorn campus.wsgi
  5. Add environment variables: DJANGO_SECRET_KEY (a newly generated key), DJANGO_ALLOWED_HOSTS (your .onrender.com address), DATABASE_URL and, if needed, PYTHON_VERSION. Leave DJANGO_DEBUG unset so debug stays off.
  6. Deploy, watch the build logs, then open your site's URL.

After the first deploy, every push to your main branch can trigger a new deploy automatically.

Option B: PythonAnywhere (a Python-focused host)

  1. Open a Bash console in your PythonAnywhere account and clone your repository with git clone.
  2. Create a virtual environment and run pip install -r requirements.txt.
  3. On the Web tab, add a new web app with manual configuration and your Python version, then enter your virtualenv path.
  4. Edit the WSGI configuration file linked on the Web tab so it loads your project (example below).
  5. Add a static files mapping: URL /static/ to the directory of your STATIC_ROOT, e.g. /home/yourusername/campus-events/staticfiles.
  6. Back in the console run python manage.py migrate, collectstatic and createsuperuser, then click Reload.

WSGI configuration file on PythonAnywhere

import os
import sys

path = "/home/yourusername/campus-events"
if path not in sys.path:
    sys.path.append(path)

os.environ["DJANGO_SETTINGS_MODULE"] = "campus.settings"
os.environ["DJANGO_SECRET_KEY"] = "paste-your-generated-key-here"
os.environ["DJANGO_ALLOWED_HOSTS"] = "yourusername.pythonanywhere.com"

from django.core.wsgi import get_wsgi_application

application = get_wsgi_application()

This file lives on the server, not in your repository, so it's an acceptable place for the secret key on PythonAnywhere. Because PythonAnywhere's disk is permanent, SQLite works well for small projects there.

After you go live

  • Create an admin user on the server (python manage.py createsuperuser in the host's console or shell). Your local users don't exist in the production database.
  • Test everything: sign up, log in, log out, create an event, RSVP, open the admin and check that CSS loads.
  • Read the logs when something breaks. With DEBUG off, error details go to your host's log viewer, not the browser.
  • Updating: push or pull the new code, install requirements, run migrate and collectstatic, then restart or reload.
Common mistake

The admin and your pages appear without any styling after deploying. This almost always means static files aren't set up: check STATIC_ROOT, run collectstatic, and make sure WhiteNoise's middleware is installed (or the static mapping is set on PythonAnywhere).

Common mistake

Getting 400 Bad Request on every page. Your domain is missing from ALLOWED_HOSTS. Check the exact address, without https:// or a trailing slash.

Common mistake

Using SQLite on a platform with an ephemeral filesystem. Everything looks fine until the next deploy, when all your users and events vanish. Use the host's PostgreSQL database instead.

Key points

  • Prepare settings with environment variables: DEBUG=False, a secret key and correct ALLOWED_HOSTS.
  • Use WhiteNoise and collectstatic to serve static files, and Gunicorn to run the app.
  • Use a database that persists: PostgreSQL on ephemeral platforms, SQLite only where the disk is permanent.
  • Render deploys from GitHub with build and start commands; PythonAnywhere uses a WSGI file and static mappings.
  • Run migrations, create a superuser and test every feature on the live site.
Up next · Going LiveProject: Build a BlogBring everything together in one project.
→

Tip: use the ← and → keys to move between pages.