← Back to Django Course | Chapter 11: Static Files & Media Handling | Lesson 4 of 8

The {% static %} Template Tag

The {% static %} tag is like asking a librarian for a book by its title instead of memorizing which shelf it's on — Django looks up the real URL for you.

Loading the static Template Tag

Before {% static %} can be used anywhere in a template, {% load static %} must appear near the top to make the tag available.

Example: Loading the static Template Tag

Before {% static %} can be used anywhere in a template, {% load static %} must appear near the top to make the tag available.

markup
{% load static %}
<!DOCTYPE html>
<html>
<head></head>
</html>
{# Django-only code -- models.py/views.py/urls.py/settings.py snippets, or template markup using Django template tags/variables -- can't run standalone via Judge0 or the browser preview, since it needs a real Django project. Only this course's pure-Python examples (example_lang == 'python', no Django imports) are actually runnable, so those still get the button below. #}

Linking a CSS File

{% static %} takes a path relative to a static/ directory and outputs the full URL, combining it with STATIC_URL automatically.

Example: Linking a CSS File

{% static %} takes a path relative to a static/ directory and outputs the full URL, combining it with STATIC_URL automatically.

markup
{% load static %}
<link rel="stylesheet" href="{% static 'myapp/style.css' %}">
{# Django-only code -- models.py/views.py/urls.py/settings.py snippets, or template markup using Django template tags/variables -- can't run standalone via Judge0 or the browser preview, since it needs a real Django project. Only this course's pure-Python examples (example_lang == 'python', no Django imports) are actually runnable, so those still get the button below. #}

Linking an Image

The same tag works for any static asset, not just CSS — images, JavaScript, and fonts all resolve the same way.

Example: Linking an Image

The same tag works for any static asset, not just CSS — images, JavaScript, and fonts all resolve the same way.

markup
{% load static %}
<img src="{% static 'myapp/logo.png' %}" alt="Logo">
{# Django-only code -- models.py/views.py/urls.py/settings.py snippets, or template markup using Django template tags/variables -- can't run standalone via Judge0 or the browser preview, since it needs a real Django project. Only this course's pure-Python examples (example_lang == 'python', no Django imports) are actually runnable, so those still get the button below. #}
Common Mistakes
  1. Forgetting {% load static %} at the top of the template before using {% static %}, causing a TemplateSyntaxError.
  2. Passing an absolute path like {% static '/style.css' %} instead of a path relative to a static/ directory.
  3. Hardcoding /static/style.css directly instead of using the tag, which breaks if STATIC_URL or a CDN prefix changes.
Chapter Summary
  • {% load static %} must appear before any use of {% static %} in a template.
  • {% static 'path/to/file' %} resolves to the correct URL using STATIC_URL automatically.
  • Using the tag instead of hardcoded paths keeps templates working even if static file hosting changes.
  • The path passed to {% static %} is relative to a static/ directory, not the filesystem root.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.