Layouts and Styling¶
What we are learning¶
- How template inheritance shares page structure.
- How blocks and navigation work.
- How static files serve CSS.
- How to keep a readable, simple responsive layout.
Why this matters¶
Without a base template every page repeats the same header and footer. One layout file fixes all pages at once.
A simple explanation before code¶
base.html holds the skeleton with named {% block %} holes. Child pages {% extends %} it and fill the holes. CSS lives in static/ and is linked with {% static %}. Django finds it because staticfiles is installed and STATIC_URL is set.
Important terms: inheritance, block, {% load static %}, static files.
Step-by-step example¶
1. Base template (20 min)¶
Create tasks/tasks/templates/base.html:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>{% block title %}TaskRat{% endblock %}</title>
{% load static %}
<link rel="stylesheet" href="{% static 'tasks/style.css' %}">
</head>
<body>
<header>
<nav>
<a href="{% url 'home_page' %}">TaskRat</a>
{% if user.is_authenticated %}
<span>Hello, {{ user.username }}</span>
<a href="{% url 'task_create' %}">Add task</a>
<form method="post" action="{% url 'logout' %}" style="display:inline;">
{% csrf_token %}
<button type="submit">Log out</button>
</form>
{% else %}
<a href="{% url 'login' %}">Log in</a>
<a href="{% url 'signup' %}">Sign up</a>
{% endif %}
</nav>
</header>
<main>
{% block content %}{% endblock %}
</main>
<footer>
<p>© TaskRat {% now 'Y' %}</p>
</footer>
</body>
</html>
Auth links render conditionally; the full auth wiring arrives in Ch7. For now anonymous learners see Log in / Sign up links that will work once Ch7 routes exist.
2. Static CSS (15 min)¶
Create tasks/tasks/static/tasks/style.css:
body {
font-family: system-ui, sans-serif;
max-width: 40rem;
margin: 0 auto;
padding: 0 1rem;
line-height: 1.5;
}
header nav {
display: flex;
gap: 1rem;
align-items: center;
padding: 1rem 0;
border-bottom: 1px solid #ccc;
}
main div {
border: 1px solid #ddd;
padding: 0.75rem;
margin: 0.75rem 0;
}
footer {
text-align: center;
color: #555;
margin-top: 2rem;
}
@media (max-width: 30rem) {
header nav {
flex-wrap: wrap;
}
}
Reload with devtools network tab: style.css returns 200. No build step, no framework.
3. Children extend it (10 min)¶
Ch3 templates already start with {% extends "base.html" %} and fill title + content. Show that deleting the nav from base.html removes it everywhere: one file, all pages.
Let's build it: follow-along practical (5 min)¶
Change the footer text once, reload two pages, confirm both changed. Narrow the browser to 400px and confirm the nav wraps.
What the code is doing¶
{% block %} names a hole with default content. {% extends %} says "use that skeleton". {% load static %} enables the {% static %} tag that builds the CSS URL. The responsive rule only wraps the nav; nothing else to learn.
Common mistakes and fixes¶
- CSS 404 — file in wrong folder. Path must be
tasks/static/tasks/style.csswith the doubledtaskssegment. - CSS not refreshing — hard refresh (Ctrl+F5 / Cmd+Shift+R).
Invalid block tag: 'static'— forgot{% load static %}.NoReverseMatchfortask_create/login— expected until Ch6/Ch7 routes exist; teach nav now, wire auth later.
Try it yourself¶
Guided: add a {% block %} sidebar. Independent: style the search input to full width on small screens.
Check your understanding¶
- What does
{% extends %}do? - What is a block?
- How does
{% static %}build a URL? - Where must app CSS live?
Summary¶
One base template plus one small stylesheet gives every page the same nav, layout, and footer.
Next lesson¶
We handle user input: HTML forms, GET versus POST, and CSRF.