Part V. Templates and Frontend¶
The Django Template Language (DTL) turns a Python context into HTML. Bootstrap 5 provides a ready-made UI system. This part covers the entire frontend stack of Notes Chat App: from base.html to the WebSocket JS client.
Prerequisites: Part II (Django Core). Level: Beginner → Intermediate.
Chapters¶
| Document | What we will learn |
|---|---|
| HTML Basics | semantic markup, forms, tables, attributes |
| CSS Basics | box model, flexbox, grid, variables, media queries |
| Django Templates | DTL syntax, {% extends %}, {% block %}, {% include %}, {{ var }}, filters |
| Advanced Templates | custom template tags, context processors, {% load %} |
| Bootstrap 5 | grid, utilities, components (card, modal, navbar, alert) |
| Django Admin | registering models, list_display, search_fields, inlines |
| Django Admin Unfold | a modernized admin with Unfold |
| Design Foundations | color, typography, spacing, UX principles |
Key concepts¶
Three-level template inheritance in Notes Chat App:
templates/base.html
← templates/layouts/dashboard.html
← notes_app/templates/notes_app/note_list.html
# base.html: DOCTYPE, head, navbar, messages, {% block content %}
# dashboard.html: {% extends 'base.html' %} + sidebar, content block
# note_list.html: {% extends 'layouts/dashboard.html' %} + the specific content
DTL syntax:
{# Variable #}
{{ note.title }}
{{ note.title|truncatechars:50 }}
{{ note.created_at|date:"d M Y" }}
{# Control flow #}
{% if note.is_pinned %}📌{% endif %}
{% for note in notes %}{{ note.title }}{% empty %}Нотаток немає{% endfor %}
{# Template inheritance #}
{% extends 'layouts/dashboard.html' %}
{% block content %}
<h1>{{ note.title }}</h1>
{% endblock %}
{# Include partial #}
{% include 'notes_app/_note_card.html' with note=note %}
{# URL reverse #}
<a href="{% url 'notes_app:note_edit' note.pk %}">Редагувати</a>
Static files:
{% load static %}
<link rel="stylesheet" href="{% static 'notes_app/css/style.css' %}">
<script src="{% static 'notes_app/js/group_chat.js' %}"></script>
# settings.py
STATIC_URL = '/static/'
STATIC_ROOT = BASE_DIR / 'staticfiles' # for collectstatic in prod
STATICFILES_DIRS = [BASE_DIR / 'notes_app' / 'static'] # dev
Context processor — variables available automatically in every template:
# settings.py TEMPLATES → context_processors:
'django.contrib.auth.context_processors.auth', # {{ user }}, {{ perms }}
'django.contrib.messages.context_processors.messages', # {{ messages }}
'django.template.context_processors.request', # {{ request }}
Where this lives in Notes Chat App¶
| File / Folder | What it shows |
|---|---|
templates/base.html |
DOCTYPE, navbar, Bootstrap 5 CDN, messages |
templates/layouts/dashboard.html |
sidebar with links, {% block content %} |
notes_app/templates/notes_app/ |
the specific templates: note_list.html, note_detail.html, ... |
notes_app/static/notes_app/js/group_chat.js |
WebSocket JS client: new WebSocket(), onmessage |
notes_app/static/notes_app/css/ |
custom styles (if any) |
WebSocket JS client (key fragment):
// group_chat.js
const ws = new WebSocket(`wss://${location.host}/ws/groups/${groupPk}/chat/`);
ws.onmessage = function(event) {
const data = JSON.parse(event.data);
if (data.type === 'history') {
// render historical messages
} else if (data.type === 'chat_message') {
appendMessage(data.author, data.content, data.timestamp);
}
};
function appendMessage(author, content, timestamp) {
const div = document.createElement('div');
// ✅ textContent — safe from XSS (not innerHTML!)
div.textContent = `${author}: ${content}`;
chatBox.appendChild(div);
}
How it connects to the rest of the course¶
Part V: Template render → HTML (static content)
Part IX: WebSocket → JS client → real-time updates (group_chat.js)
collectstatic (Part V) → Nginx serves /static/ (Part X)
Zero to Hero: Step 4 (crispy_notes) — a Bootstrap 5 SaaS dashboard, template inheritance.
Practice¶
- Open
templates/base.html→ trace the{% block %}chain down tonote_list.html. - Add a new
{% block %}indashboard.htmlfor a sidebar badge (the number of notes). - Create a custom template filter
{% load note_extras %}→{{ note.title|shorten }}. - Run
docker compose exec web python manage.py collectstatic --noinput→ look throughstaticfiles/.
Review questions¶
- What is the difference between
{% extends %}and{% include %}? - Why is
{% url 'notes_app:note_edit' note.pk %}better than a hard-coded/notes/42/edit/? - What is a context processor? Where does
{{ user }}in a template come from? - Why do you need
collectstaticin production? Who serves static files in Notes Chat App? - Why does
group_chat.jsusetextContentrather thaninnerHTML? (XSS!) - Where should templates live — in
templates/or innotes_app/templates/notes_app/? What is the difference?
Next → Part VI. Application Architecture — how to split responsibilities between layers.