liveBook · Manning

Load tag

As explained in Django in Action

The Django load tag imports template tag modules into a template's namespace.

The {% load %} tag imports template tag modules into a template’s namespace, much like Python’s import statement. Use it when a useful tag lives outside Django’s default tag library. section 7.3

For example, {% static %} is supplied by Django but must be loaded separately. Put {% load static %} near the top of the template, then use {% static 'img/logo.png' %} to produce the file’s URL. section 7.3

Loading static in the RiffMates home page

The template extends base.html, loads the static tag library, and places the generated image URL inside an <img> element. The static file name is relative to the static directory. section 7.3

Listing 7.13 Template for the home page that includes a static file
<!-- RiffMates/templates/home.html -->
{% extends "base.html" %}
{% load static %}  #1

{% block content %}
  <img src="{% static 'img/logo.png' %}" height="100">  #2
  <h1>Welcome to RiffMates!</h1>

  <ul>  #3
    <li> <a href="{% url 'news' %}">News</a> </li>
    <li> <a href="{% url 'musicians' %}">Musicians</a> </li>
    <li> <a href="{% url 'bands' %}">Bands</a> </li>
    <li> <a href="{% url 'venues' %}">Venues</a> </li>
    <li> <a href="{% url 'list_ads' %}">Classified Ads</a> </li>
    <li> <a href="{% url 'comment' %}">Leave a comment</a> </li>
  </ul>

{% endblock content %}

Inspect the complete home-page template showing {% load static %} and its use with the logo image. section 7.3

How Django in Action covers it

How deep it goes
Explains namespace behavior through a Python import analogy and demonstrates separate loading, without descending into implementation internals.
Examples
Demonstrates {% load static %} in a home template and applies {% static 'img/logo.png' %} to an image.
What it assumes
Assumes template-tag syntax and Python imports, while distinguishing default tags from libraries that require explicit loading.
What you can do after
Apply it via Django templates to make separately packaged template tags available for use.
How it connects
Connects template namespaces with Python imports, Django’s static tag, static files, and default template tag libraries.

This explainer was generated by Manning's knowledge graph from the text of Django in Action. The section links point to the parts of the book it draws on.