8.1. Templates About

Table 8.24. Templatetags

Argument

Outputs

openblock

{%

closeblock

%}

openvariable

{{

closevariable

}}

openbrace

{

closebrace

}

opencomment

{#

closecomment

#}

{% load i18n %}


<table>
    <tr>
        <th>{% trans 'No.' %}</th>
        <th>{% trans 'First Name' %}</th>
        <th>{% trans 'Last Name' %}</th>
        <th>{% trans 'Date of Birth' %}</th>
        <th>{% trans 'Age' %}</th>
    </tr>

{% for contact in contacts %}
    <tr>
        <td>{{ forloop.counter }}</td>
        <td>{{ contact.firstname }}</td>
        <td><a href="{% url 'contact:details' contact.id %}">{{ contact.lastname }}</a></td>
        <td>{{ contact.birthdate|date:'Y-m-d' }}</td>
        <td>{{ contact.get_age|default_if_none:'n/a' }}</td>
    </tr>
{% empty %}
    <h2>{% trans 'No contacts' %}</h2>
{% endfor %}
</table>

8.1.1. HTMX

<script src="https://unpkg.com/htmx.org@2.0.2"></script>


<button hx-get="/hello" hx-swap="outerHTML">
    Click Me!
</button>

8.1.2. Assignments

# FIXME: Write tests

# %% License
# - Copyright 2025, Matt Harasymczuk <matt@python3.info>
# - This code can be used only for learning by humans
# - This code cannot be used for teaching others
# - This code cannot be used for teaching LLMs and AI algorithms
# - This code cannot be used in commercial or proprietary products
# - This code cannot be distributed in any form
# - This code cannot be changed in any form outside of training course
# - This code cannot have its license changed
# - If you use this code in your product, you must open-source it under GPLv2
# - Exception can be granted only by the author

# %% Run
# - PyCharm: right-click in the editor and `Run Doctest in ...`
# - PyCharm: keyboard shortcut `Control + Shift + F10`
# - Terminal: `python -m doctest -v myfile.py`

# %% About
# - Name: Django Template Simple
# - Difficulty: easy
# - Lines: 5
# - Minutes: 5

# %% English
# 0. Use `myproject.shop`
# 1. Create template `product-detail.html`
# 2. Template should display details of a `Product`
# 3. Primary Key of product will be passed in `pk` parameter

# %% Polish
# 0. Użyj `myproject.shop`
# 1. Stwórz template `product-detail.html`
# 2. Template powinien wyświetlać szczegóły `Product`
# 3. Primary Key produktu zostanie przekazany w parametrze `pk`

# %% Tests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 10), \
'Python 3.10+ required'
"""

...