Easier to read with cream paper, a tint, more contrast or larger text? Choose your colours and text, once, for every page.

Free lesson

Task 2 A: One layout for every page

The whole lesson, free. Read it and have a go at the activities. To work the tasks and labs in the real lesson, press Start lab, with no account needed.

60 minutes

A quick check that you are not a bot, then your own lab opens on the interactive lesson.

Already have an account? Sign in

Same shirt, every player

Every player in a team wears the same shirt. Only the number on the back changes.

Web pages in one app should be the same. The header, the menu and the footer stay put. Only the middle changes.

If you copy the header into twenty pages, one small change means twenty edits. There is a better way.

Key words

WordWhat it meansExample
TemplateA page with gaps that the app fills in.home.html
JinjaThe template language Flask uses.
Base templateThe one layout every other page extends.
BlockA named gap in a template that a page fills.
Skip linkA hidden link that jumps a keyboard user to the main content.
AccessibilityMaking an app work for everyone, including disabled people.

The cells students fill in are left blank in this preview.

By the end of this lesson

Note

I can build a base template that every page of the app extends.

Starter: what you already know

Have a go

Starter: what you already know

3 questions
Have a go: your answers are marked here and then forgotten. Sign in if you would like your score kept, and to pick up where you left off.Sign in to keep your score

Sprint 0, step 2: the goal

PartFor this step
Sprint (a short block of work)Sprint 0: a project that runs, a page layout and sample data.
This stepA base template, a home page and one stylesheet.
Story it startsE6.3: as a keyboard user, I want to reach every link and button, so that I can use the app without a mouse.

The cells students fill in are left blank in this preview.

A template is a page with gaps

Flask keeps HTML in templates, in a folder called templates. The language inside them is Jinja.

{{ ... }} prints a value. {% ... %} is a command, such as for or if.

The base template holds the parts every page shares. It leaves named gaps, called blocks. Each page says {% extends "base.html" %} and fills the blocks. Change the menu once and every page changes.

Step 2: the base template

Code: jinja
{# helpdesk/templates/base.html (part) #}
<html lang="en-GB">
<head>
  <title>{% block title %}Home{% endblock %} | Netheret help desk</title>
  <link rel="stylesheet" href="{{ url_for('static', filename='css/style.css') }}">
</head>
<body>
  {# The skip link is the first thing a keyboard user reaches. #}
  <a class="skip-link" href="#main">Skip to main content</a>
  <header class="site-header"> ... the brand and the menu ... </header>
  <main id="main" class="wrap" tabindex="-1">
    {% block content %}{% endblock %}
  </main>
  <footer class="site-footer"> ... </footer>
</body>
</html>

Check: templates and blocks

Have a go

Check: templates and blocks

3 questions
Have a go: your answers are marked here and then forgotten. Sign in if you would like your score kept, and to pick up where you left off.Sign in to keep your score

Step 2: a page that extends it

Code: jinja
{# helpdesk/templates/home.html (part) #}
{% extends "base.html" %}
{% block title %}Home{% endblock %}
{% block content %}
  <h1>Netheret help desk</h1>
  <p class="lead">Log support requests, plan on-site jobs and see how the help desk is doing.</p>
{% endblock %}

The route now returns a template

In step 1 the home page returned some HTML as text. Now it calls render_template("home.html"). Flask finds the file, fills the blocks and sends the page.

Python decides what to show. The template decides how it looks. Keeping the two apart makes the code easier to read and to change.

Step 2: home() uses render_template

Code: python
# helpdesk/main.py, step 2
from flask import Blueprint, render_template

bp = Blueprint("main", __name__)


@bp.route("/")
def home():
    # render_template finds home.html in the templates folder.
    return render_template("home.html")

Make a free account to save your work and track your progress.

Match each part to its job

Have a go

Loading…

Your answers are marked here and are not kept. A college account keeps every student’s marks.

A common mistake

Warning

Many students copy the header and menu into every page. It works at first. Then one menu change means editing every page, and some get missed. Put shared parts in the base template once. A page should only hold what is different about it.

Accessible from the very first page

Some people use only a keyboard. They press Tab to move from link to link.

The skip link is the first thing Tab reaches. It jumps past the menu to the main content. It stays hidden until it gets focus.

A yellow focus ring shows where you are. lang="en-GB" tells a screen reader which voice to use. All the colours are CSS variables (named values set once), so they change in one place.

Try it: press Tab

Code: web
<a class="skip-link" href="#main">Skip to main content</a>
<header class="site-header">
  <strong>Netheret</strong> help desk
  <nav><a href="#" aria-current="page">Home</a> <a href="#">Sign in</a></nav>
</header>
<main id="main" tabindex="-1">
  <h1>Netheret help desk</h1>
  <p>Click in this page, then press Tab. Watch the top left corner.</p>
  <p><a href="#">A link to try the focus ring on</a></p>
</main>

Make a free account to save your work and track your progress.

Check: the accessible layout

Have a go

Check: the accessible layout

3 questions
Have a go: your answers are marked here and then forgotten. Sign in if you would like your score kept, and to pick up where you left off.Sign in to keep your score

Build it yourself: step 2

Build step 2. 1. Make helpdesk/templates/base.html with a skip link, a header with a menu, a main area with a content block, and a footer. 2. Make home.html that extends it and fills the title and content blocks. 3. Make helpdesk/static/css/style.css with your colours as variables, a hidden skip link and a clear focus ring. 4. Change home() to use render_template. 5. Test it: press Tab on the page and check the skip link and the focus ring appear. 6. Commit and write the change log row.

A teacher marks this in a college. Nothing you write here is kept: it goes when you leave the page.

Make a free account to save your work, or press Start lab to work through the whole lesson.

Compare with the step 2 code

Tip

Compare yours with the step 2 code on GitHub. Look at style.css too: every colour is a variable at the top.

Change log entry for step 2

DateVersion and filesWhat changedWhy
3 Marchstep-02: base.html, home.html, style.css, main.pyA base template with header, menu, main and footer, a skip link and a focus ringEvery later page gets the same accessible layout without repeating it

The cells students fill in are left blank in this preview.

Next: the database

The pages have nowhere to get data from yet. In step 3 you turn the Task 1 data design into Python classes, one for each table. A script fills the tables with sample data, so every later page has something real to show.

Exit quiz

Have a go

Exit quiz

5 questions
Have a go: your answers are marked here and then forgotten. Sign in if you would like your score kept, and to pick up where you left off.Sign in to keep your score

Teach this lesson with your class

On a college account your students work the tasks and the labs as well, and every mark is kept for you.