Lesson 21 of 60 – Django Static Files
35%

Django Static Files

Static files are files such as CSS, JavaScript, images, fonts, and other assets used to design and enhance a Django website. Django provides a dedicated static files system to manage these files.

Note: Static files are different from dynamic data. CSS, JavaScript, and website images are commonly treated as static files.

1. What are Static Files?

Static files are files that are generally served to the browser without being generated dynamically for every request.

  • CSS files
  • JavaScript files
  • Images
  • Fonts
  • Icons

2. Why Use Static Files?

Static files are used to improve the appearance and functionality of a website.

  • CSS controls page design.
  • JavaScript adds browser-side functionality.
  • Images improve visual presentation.
  • Fonts improve typography.
  • Icons provide visual controls.

3. Static Files in a Django Project

A common app-level structure can look like this:

myapp/
│
├── views.py
├── models.py
│
├── static/
│   └── myapp/
│       ├── css/
│       │   └── style.css
│       ├── js/
│       │   └── script.js
│       └── images/
│           └── logo.png
│
└── templates/
    └── home.html

4. STATIC_URL Setting

Django uses STATIC_URL to define the URL prefix used when serving static files.

STATIC_URL = 'static/'

This setting is commonly present in the Django project's settings.py.

5. The static Template Tag

Django provides the static template tag for generating URLs for static files.

{% load static %}

After loading it, a static file can be referenced using:

{% static 'myapp/css/style.css' %}

6. Loading the Static Library

Before using the static template tag in a template, load the static template tag library.

{% load static %}

This is normally placed near the top of the template.

7. Linking a CSS File

{% load static %}

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

Django generates the appropriate static URL for the CSS file.

8. Adding JavaScript

{% load static %}

<script src="{% static 'myapp/js/script.js' %}">
</script>

This allows a JavaScript file to be loaded from the static files system.

9. Displaying an Image

{% load static %}

<img src="{% static 'myapp/images/logo.png' %}"
     alt="Logo">

The same static mechanism can be used for images.

10. STATICFILES_DIRS

STATICFILES_DIRS can specify additional directories where Django should look for static files.

STATICFILES_DIRS = [
    BASE_DIR / "static",
]

This is useful when a project has a project-level static directory.

11. Project-Level Static Directory

project/
│
├── manage.py
├── static/
│   ├── css/
│   ├── js/
│   └── images/
│
├── myproject/
└── myapp/

The project-level directory can be configured through STATICFILES_DIRS.

12. App-Level Static Directory

Static files can also be stored inside individual Django applications.

myapp/
└── static/
    └── myapp/
        ├── css/
        ├── js/
        └── images/

This helps keep an app's static resources together with the app.

13. Why Use an App Namespace?

Using an application-specific directory inside static helps avoid name conflicts between files from different applications.

static/
└── students/
    └── css/
        └── style.css

14. STATIC_ROOT

STATIC_ROOT specifies the directory where static files are collected when the collectstatic command is used.

STATIC_ROOT = BASE_DIR / "staticfiles"

It is especially important when preparing static files for deployment.

15. collectstatic Command

Django provides the collectstatic management command to collect static files into the configured STATIC_ROOT directory.

python manage.py collectstatic

The collected files can then be served by an appropriate production setup.

16. Static Files During Development

During development, Django can serve static files when DEBUG = True and the static files application is configured.

This makes it convenient to test CSS, JavaScript, and images while developing.

17. django.contrib.staticfiles

Django's static files support is provided by the django.contrib.staticfiles application.

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

It is normally included in a new Django project.

18. Static Files in base.html

If many pages share the same CSS or JavaScript, static files can be referenced from the base template.

{% load static %}

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

{% block content %}
{% endblock %}

19. Static CSS Example

style.css

body {
    font-family: Arial, sans-serif;
}

h1 {
    color: blue;
}

Template:

{% load static %}

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

20. Static JavaScript Example

script.js

function showMessage() {
    alert("Hello Django!");
}

Template:

{% load static %}

<script src="{% static 'myapp/js/script.js' %}">
</script>

<button onclick="showMessage()">
Click Me
</button>

21. Static Image Example

{% load static %}

<img
    src="{% static 'myapp/images/logo.png' %}"
    alt="My Logo"
>

The image file is stored inside the static directory and referenced through the static template tag.

22. Static Files and Template Inheritance

Static files work well with template inheritance. A base template can load common CSS and JavaScript files once.

{% extends 'base.html' %}

{% block content %}

<h1>Home Page</h1>

{% endblock %}

The child template automatically uses the static resources included by the base layout.

23. Static URL vs Media URL

Static files and media files are different types of resources.

Static Files Media Files
CSS User-uploaded images
JavaScript User-uploaded documents
Website icons Profile photos
Website assets User-generated files

24. Common Static File Settings

STATIC_URL = 'static/'

STATICFILES_DIRS = [
    BASE_DIR / "static",
]

STATIC_ROOT = BASE_DIR / "staticfiles"

The exact configuration can vary depending on the project and deployment setup.

25. Static File Path

A static file path should match its location inside the configured static directories.

static/
└── css/
    └── style.css

The template can reference it as:

{% static 'css/style.css' %}

26. Common Static File Errors

  • Forgetting {% load static %}
  • Incorrect static file path
  • Incorrect STATIC_URL
  • Static files stored outside configured directories
  • Incorrect STATICFILES_DIRS
  • Forgetting to run collectstatic for a production setup
  • Browser cache making old CSS or JavaScript appear

27. Recommended Development Structure

project/
│
├── manage.py
│
├── static/
│   ├── css/
│   │   └── style.css
│   ├── js/
│   │   └── script.js
│   └── images/
│       └── logo.png
│
├── myproject/
│   └── settings.py
│
└── myapp/
    ├── views.py
    └── templates/
        └── home.html

28. Static Files Workflow

1. Create static directory
        ↓
2. Add CSS, JS, images, etc.
        ↓
3. Configure static settings
        ↓
4. Load static library
        ↓
5. Use {% static %} in templates
        ↓
6. Test during development
        ↓
7. Run collectstatic for deployment

29. Complete Static Files Example

settings.py

STATIC_URL = 'static/'

STATICFILES_DIRS = [
    BASE_DIR / "static",
]

Folder:

static/
├── css/
│   └── style.css
├── js/
│   └── script.js
└── images/
    └── logo.png

home.html

{% load static %}

<!DOCTYPE html>
<html>

<head>

    <title>Home</title>

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

</head>

<body>

    <img
        src="{% static 'images/logo.png' %}"
        alt="Logo"
    >

    <h1>Welcome to Django</h1>

    <script src="{% static 'js/script.js' %}">
    </script>

</body>

</html>

30. Summary of Django Static Files

Django provides a structured way to manage CSS, JavaScript, images, and other static resources used by a website.

  • Static files include CSS, JavaScript, images, and fonts.
  • STATIC_URL defines the static URL prefix.
  • {% load static %} loads Django's static template tags.
  • {% static %} generates a static file URL.
  • STATICFILES_DIRS can define additional static directories.
  • STATIC_ROOT is used as the collection directory.
  • collectstatic collects static files for deployment.
  • Static files can be stored at project or app level.

📌 Key Points

  • Static files include CSS, JavaScript, images, fonts, and icons.
  • Use STATIC_URL to define the static URL prefix.
  • Use {% load static %} before using the static template tag.
  • Use {% static 'path/to/file' %} to reference static files.
  • STATICFILES_DIRS can point Django to additional static directories.
  • STATIC_ROOT is commonly used with collectstatic.
  • Static files and user-uploaded media files are different.
  • Keep static file paths organized to avoid conflicts.

🧠 Quick Quiz

Question: Which Django template tag is used to generate the URL for a static file?