← Back to Django Course | Chapter 3: Templates & Template Language | Lesson 8 of 10

Linking Static Files in Templates

The {% static %} tag builds the correct web address for your CSS, JavaScript, and image files so links never break.

Loading the static Template Tag

Before using {% static %}, a template must load the static tag library with {% load static %}, typically at the very top of the file.

Warning: Forgetting {% load static %} causes a TemplateSyntaxError: static is not a registered tag.

Example: Loading the static Template Tag

{% load static %} makes the {% static %} tag available for the rest of this template.

markup
<!-- templates/base.html -->
{% load static %}
<!DOCTYPE html>
<html>
<head></head>
<body></body>
</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 path %} resolves to the correct URL for a file inside any app's static/ folder, combined with STATIC_URL.

Example: Linking a CSS File

This renders as href="/static/core/css/style.css" (or wherever STATIC_URL points), instead of a hardcoded path.

markup
{% load static %}
<link rel="stylesheet" href="{% static 'core/css/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 with static

The same {% static %} tag works for any static asset, including images referenced in <img> tags.

Example: Linking an Image with static

The logo's URL is generated dynamically, so it stays correct even if STATIC_URL changes for production.

markup
{% load static %}
<img src="{% static 'core/images/logo.png' %}" alt="Site 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. Hardcoding a static file path like '/static/style.css' instead of using {% static %}, which breaks once STATIC_URL or deployment paths change.
  2. Forgetting {% load static %} at the top of the template before using the {% static %} tag anywhere in it.
  3. Putting quotes incorrectly around the path, e.g. {% static css/style.css %} without quotes, which raises a TemplateSyntaxError.
Chapter Summary
  • {% load static %} must appear before any use of {% static %} in a template.
  • {% static 'path/to/file' %} generates the correct URL for a static asset based on STATIC_URL.
  • Using {% static %} instead of hardcoded paths keeps templates correct across development and production.

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.