Django
Intermediate
59 helpful
1519 views

Resolve Django CORS Issues in Development

Published Mar 15, 2025 Last updated Jun 16, 2025

Problem

Frontend application cannot make API requests to Django backend due to CORS (Cross-Origin Resource Sharing) errors in development environment.

Root Cause

CORS errors occur when a web application running on one domain tries to access resources from another domain. In development, this commonly happens when your frontend (e.g., React on localhost:3000) tries to access your Django API (e.g., localhost:8000).

Solution

To fix CORS issues in Django, install and configure django-cors-headers:

Step 1: Install django-cors-headers

pip install django-cors-headers

Step 2: Add to INSTALLED_APPS in settings.py

INSTALLED_APPS = [
    # ... other apps
    'corsheaders',
    # ... rest of your apps
]

Step 3: Add middleware (must be at the top)

MIDDLEWARE = [
    'corsheaders.middleware.CorsMiddleware',
    'django.middleware.common.CommonMiddleware',
    # ... other middleware
]

Step 4: Configure CORS settings

# For development only - allow all origins
CORS_ALLOW_ALL_ORIGINS = True

# For production - specify allowed origins
CORS_ALLOWED_ORIGINS = [
    "http://localhost:3000",
    "http://127.0.0.1:3000",
    "https://yourdomain.com",
]

# Allow credentials if needed
CORS_ALLOW_CREDENTIALS = True

# Specify allowed headers
CORS_ALLOW_HEADERS = [
    'accept',
    'accept-encoding',
    'authorization',
    'content-type',
    'dnt',
    'origin',
    'user-agent',
    'x-csrftoken',
    'x-requested-with',
]
Back to Solutions
1