Skip to content

Django Static File 404 Error Fix

DodaTech Updated 2026-06-24 1 min read

In this tutorial, you'll learn about Django Static File 404 Error Fix. We cover key concepts, practical examples, and best practices.

The Problem

Django templates load static files but the browser returns 404. In development, DEBUG=False is usually the cause. In production, collectstatic has not been run.

Quick Fix

Step 1: Check settings

STATIC_URL = '/static/'
STATIC_ROOT = BASE_DIR / 'staticfiles'
STATICFILES_DIRS = [BASE_DIR / 'static']

Step 2: Run collectstatic

python manage.py collectstatic

Step 3: Ensure staticfiles in INSTALLED_APPS

INSTALLED_APPS = [
    'django.contrib.staticfiles',
    ...
]

Step 4: Serve in development

from django.conf import settings
from django.conf.urls.static import static

if settings.DEBUG:
    urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

Step 5: Use static template tag

{% load static %}
<link rel="stylesheet" href="{% static 'css/style.css' %}">

Prevention

  • Always use {% static %} tag, never hardcode URLs.
  • Set up STATIC_ROOT and run collectstatic in deployment.
  • Configure Nginx/Apache to serve STATIC_ROOT.

Common Mistakes with static 404

  1. Forgetting deriving (Show, Eq) on custom data types needed for debugging
  2. Placing the wildcard pattern first in case expressions, making all subsequent patterns unreachable
  3. Using head and tail instead of pattern matching, causing runtime errors on empty lists

These mistakes appear frequently in real-world DJANGO code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.

Practice Exercise

Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.

This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.

FAQ

### Why do static files work in development but not production?

In development with DEBUG=True, Django serves them. In production, the web server must serve STATIC_ROOT.

How to configure Nginx for Django static files?

location /static/ { alias /path/to/staticfiles/; }

What is the difference between STATIC_URL and STATIC_ROOT?

STATIC_URL is the URL prefix. STATIC_ROOT is the filesystem path where collectstatic copies files.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro