NENnamdi E.
Back to all posts
tutorialsTuesday, May 27, 20254 min read

How to Deploy a Django API to PythonAnywhere (Step-by-Step)

Deploying your Django API to PythonAnywhere doesn’t have to be complicated. In this guide, you'll learn how to set up your environment, upload your project, configure your settings, and go live, all without leaving your browser. Whether you're a beginner or just looking for a quick deployment solution, this step-by-step tutorial has you covered.

PythonPythonAnywhereREST APIFree HostingDjangoBackend Development
deploy-to-pythonaywhere

So you’ve built a Django API and want to share it with the world, but setting up cloud servers or Docker feels overwhelming? Good news: PythonAnywhere offers a simple and beginner-friendly way to deploy Django projects, including APIs, without needing to manage infrastructure.

In this tutorial, I’ll walk you through deploying a Django REST API to PythonAnywhere using either Git or a manual upload. Whether you're testing out a side project or launching a small production app, this guide will help you get your API online quickly and reliably, with zero DevOps experience required.

Prerequisites

  • A Django project with API functionality
  • A PythonAnywhere account (free tier works for small projects)
  • Git repository with your project (recommended)

Step 1: Set Up Your PythonAnywhere Account

  1. Sign up or log in to PythonAnywhere
  2. From your dashboard, click on the “Web” tab
  3. Click on “Add a new web app”
  4. Select “Manual configuration”
  5. Choose the Python version matching your project (e.g., Python 3.8)

Step 2: Upload Your Django Project

Option A: Using Git (Recommended)

  1. Open a Bash console from your PythonAnywhere dashboard
  2. Clone your repository:
1git clone https://github.com/yourusername/your-repo.git

Option B: Manual Upload

  1. Zip your Django project locally
  2. From your dashboard, go to the “Files” tab
  3. Upload the zip file
  4. Use the Bash console to unzip:
1unzip your-project.zip

Step 3: Set Up a Virtual Environment

  1. In your Bash console, navigate to your project directory
  2. Create a virtual environment:
1python -m venv venv
  1. Activate the virtual environment:
1source venv/bin/activate
  1. Install your project dependencies:
1pip install -r requirements.txt
  1. If you don’t have a requirements.txt, install Django and other dependencies:
1pip install django djangorestframework gunicorn

Step 4: Configure Your Web App

  1. Go to the “Web” tab in your dashboard
  2. Under “Code” section:
  3. Set the source code directory to your project folder
  4. Set the working directory to your project folder
  5. Under “WSGI configuration file”, click on the link to edit it
  6. Replace the contents with:
1import os
2import sys
3
4# Add your project directory to the sys.path
5path = '/home/yourusername/your-project-directory'
6if path not in sys.path:
7 sys.path.append(path)
8
9# Set environment variables
10os.environ['DJANGO_SETTINGS_MODULE'] = 'yourproject.settings'
11
12# Create application object
13from django.core.wsgi import get_wsgi_application
14application = get_wsgi_application()
  1. Save the file

Step 5: Configure Django Settings

  1. Create a new file local_settings.py in your project’s settings directory:
1import os
2
3# SECURITY WARNING: keep the secret key used in production secret!
4SECRET_KEY = 'your-secret-key'
5
6# SECURITY WARNING: don't run with debug turned on in production!
7DEBUG = False
8
9ALLOWED_HOSTS = ['yourusername.pythonanywhere.com']
10
11# Database
12# Use SQLite for simplicity on PythonAnywhere
13DATABASES = {
14 'default': {
15 'ENGINE': 'django.db.backends.sqlite3',
16 'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
17 }
18}
19
  1. Import this file in your main settings.py:
1try:
2 from .local_settings import *
3except ImportError:
4 pass

Step 6: Set Up Static Files

  1. In the “Web” tab, under “Static files”:
  2. Add a new mapping with URL: /static/ and Directory: /home/yourusername/your-project-directory/static
  3. Add another mapping for media files if needed

Step 7: Run Migrations and Collect Static Files

  1. In your Bash console, with the virtual environment activated:
1python manage.py migrate
2python manage.py collectstatic

Step 8: Configure CORS (if your API will be accessed from other domains)

  1. Install django-cors-headers:
1pip install django-cors-headers
  1. Add to your INSTALLED_APPS in settings.py:
1INSTALLED_APPS = [
2 # ...
3 'corsheaders',
4 # ...
5]
  1. Add to your MIDDLEWARE in settings.py:
1MIDDLEWARE = [
2 # ...
3 'corsheaders.middleware.CorsMiddleware',
4 'django.middleware.common.CommonMiddleware',
5 # ...
6]
7
  1. Configure CORS settings in settings.py:
1CORS_ALLOW_ALL_ORIGINS = False
2CORS_ALLOWED_ORIGINS = [
3 "https://example.com",
4 "https://app.example.com",
5]

Step 9: Reload Your Web App

  1. Go back to the “Web” tab
  2. Click the “Reload” button for your web app

Step 10: Test Your API

  1. Visit https://yourusername.pythonanywhere.com/your-api-endpoint/ to verify your API is working
  2. Check the error logs if there are any issues

Common Issues and Solutions

502 Bad Gateway

  • Check your WSGI file for errors
  • Ensure virtual environment has all required packages
  • Review error logs in the “Web” tab

Static Files Not Loading

  • Verify STATIC_ROOT and STATIC_URL settings
  • Run python manage.py collectstatic again
  • Check static files configuration in PythonAnywhere

Database Issues

  • For SQLite: check file permissions
  • For MySQL: verify connection settings

Conclusion

Your Django API should now be successfully deployed to PythonAnywhere. For production environments, consider:

  • Using MySQL instead of SQLite
  • Setting up proper security measures
  • Configuring a custom domain
Share
Nnamdi Ekechi
Nnamdi Ekechi
Full-Stack Software Engineer

Helping businesses build secure, scalable backend systems. Specialized in React, Next.js, Django, and DRF.

Let's work together