Templates and the Template Language
The template is the HTML the render: the {% extends %} (the block, the base, the no the repeat), the {{ }} (the variable, the escape the XSS), the {% for %} (the loop, the empty), and the filter (the 20, the date, the length, the truncate). The {% include %} is the partial (the nav, the footer), the {% url %} is the reverse (the name, the no the hardcode), and the custom tag is the re-use (the library, the {% load %}). This chapter covers the {% extends %} (the 2 the level, the block), the filter (the 10 the use), the {% for %} (the loop, the forloop), the {% include %} (the partial), and the custom tag (the simple_tag).
The extends and the filter
<!-- the BASE (the 1, the no the repeat) -->
<!-- base.html -->
<!doctype html>
<html><head>
<title>Notes</title>
<link rel="stylesheet"
href="{% static 'css/site.css' %}">
</head><body>
{% include "notes/_nav.html" %}
<main>
{% block content %}{% endblock %}
</main>
{% include "notes/_footer.html" %}
</body></html>
<!-- the CHILD (the 2, the block's) -->
<!-- notes/list.html -->
{% extends "base.html" %}
{% block content %}
<h1>Notes {{ notes|length }}</h1>
{% for note in notes %}
<article>
<a href="{% url 'note-detail'
note.pk %}">{{ note.title
|truncatechars:50 }}</a>
<time datetime="{{ note.created
|date:'c' }}">
{{ note.created|date:"j M Y" }}
</time>
</article>
{% empty %}
<p>the no the note</p>
{% endfor %}
{% endblock %}
The {% extends %} is the 2 the level: the base.html is the 1 (the nav, the {% block content %}), and the list.html is the block (the no the repeat, the 20 the page the 1 the base) - the {% extends %} is the 1 (the 1st the line, the no the the), and the {% block %} is the fill (the no the block the empty). The {{ }} is the escape (the < the <, the XSS the no, the |safe is the no the trust the no), and the filter is the 10: the date (the j M Y, the format), the length (the count), the truncatechars (the 50, the no the width), the center, the upper the lower, the default (the empty the 1), the add (the 1 the plus), the join (the list the string), the dictsort, and the safe (the HTML, the trust) - the {% for %} is the loop: the {% empty %} is the no (the 1 the no the line), the forloop.counter is the 1 the index, the forloop.first the last the flag, and the {% widthratio %} is the bar - the {% include %} is the next: the partial the no the block.
The include, the url, and the custom tag
<!-- the INCLUDE (the partial, the no
the block) -->
<!-- notes/_nav.html -->
<nav>
<a href="{% url 'note-list' %}"
{% if q %}class="active"{% endif %}
>Notes</a>
<form method="get">
<input name="q" value="{{ q }}">
<button>Find</button>
</form>
</nav>
<!-- the 2: the include the context
the no (the q, the no the 1) -->
<!-- the if (the no the loop, the
the no the math) -->
{% if notes %}
<h1>{{ notes|length }} notes</h1>
{% elif q %}
<p>the no the {{ q }}</p>
{% else %}
<p>the no the note</p>
{% endif %}
<!-- the CUSTOM TAG (the library) -->
<!-- notes_app/templatetags/note_extras.py -->
# from django import template
# reg = template.Library()
#
# @reg.simple_tag
# def word_count(text):
# return len(text.split())
#
# @reg.filter
# def highlight(text, word):
# return text.replace(word,
# f"<mark>{word}</mark>")
<!-- the use (the load, the no the
the import): -->
{% load note_extras %}
{{ note.body|word_count }}
{{ note.body|highlight:q|safe }}
<!-- the the no: the safe (the HTML's
the mark, the no the escape) -->
The {% include %} is the partial: the _nav.html is the 1 (the 20 the page the no the copy), the context is the pass (the q, the no the 1), and the {% only %} is the no the context (the explicit, the no the the) - the {% if %} is the logic (the elif, the else, the no the math, the no the method), and the {% url %} is the reverse (the name, the args, the no the hardcode, the the the change the the the no). The custom tag is the library: the templatetags/note_extras.py is the 1 the file (the template.Library), the @simple_tag is the 1 the function (the {% word_count %}), the @filter is the 2 the arg (the text plus the word), and the {% load %} is the 1 the line (the no the import) - the the is the no the logic (the View the math, the template the render), and the rule is the one: the extends the block, the filter the 10, the for the loop, the include the partial, and the custom tag the library - the next chapter's static is the file the collect the nginx.
The Working Rules
- The {% extends %} is the 1 line, the block the no repeat. The base.html the 1 (the nav, the block content), and the child the block (the 20 the page the 1 the base) - the extends the 1st the line (the no the the), and the no the block (the empty, the no the inherit).
- The {{ }} is the escape, the |safe the trust. The < the < (the XSS the no), and the filter the 10 (the date, the length, the truncatechars, the default, the join, the safe) - the no the safe (the HTML the trust, the no the user's), and the no the math (the View, the no the template).
- The {% for %} is the loop, the forloop the 4. The {% empty %} the no (the 1 the line), the forloop.counter the 1 the index, and the first the last the flag - the no the break (the slice, the [:5] the no), and the no the counter (the range, the no the 1).
- The include is the partial, the custom tag the library. The _nav.html the 1 (the 20 the page the no the copy), and the {% only %} the no context (the explicit) - the simple_tag the function (the {% %}), the filter the 2 (the |), and the {% load %} the 1 the line (the no the import).
Summary
The template is the HTML the render: the {% extends %} the 2 (the base the 1, the child the block, the no the repeat, the extends the 1st the line), the {{ }} the escape (the XSS the no, the |safe the trust), the filter the 10 (the date, the length, the truncatechars, the default, the add, the join, and the safe), the {% for %} the loop (the empty the no, the forloop.counter the first the last, the no the break), the {% include %} the partial (the no the block, the context the pass, the {% only %} the no), and the custom tag the library (the templatetags the 1 file, the simple_tag the function, the filter the 2, the {% load %} the no the import). The rule is the one: the template the no the logic (the View the math, the template the render) - the next chapter's static is the file: the {% static %} the tag, the staticfiles the INSTALLED_APPS, and the collectstatic the nginx the no the Django.