Split an app into model, view and controller, and design views people can use.
Lectures this week
- Overview of MVC
- Views
- User Interface Design
- Usability Heuristics
- Tools – Part I
- Tools – Part II
- Accessibility
Key points, notes and past questions
Templating & HTML Generation
Turning data into HTML with Template, PyHTML and Jinja2.
- Start here: why templates, from zero
- A template is HTML with blanks; the context fills them.
- string.Template:
$name; Jinja2: {{ }} and {% %}; PyHTML: nested calls. - Templates keep HTML readable and, in Flask, escape user text.
- Python's string.Template
$nameplaceholders.- substitute: missing value → KeyError.
- safe_substitute: missing value stays as
$name. - Extra keys are ignored by both.
- PyHTML: HTML from nested Python calls
- Each tag is a function.
- Arguments = children.
- Separate arguments = siblings.
render()returns the HTML text.
- Jinja2 expressions, loops and macros
{{ }}prints;{% %}controls.- A for loop repeats its body per item.
- Macros are template functions.
- Each
<p>shows on its own line.
- Macros and {% set %} scope
{% set %}inside a for loop stays in the loop.- After the loop the variable is undefined → empty.
- Use
namespaceto carry values out.
- Template inheritance
{% extends "file" %}first.{% block name %}…{% endblock %}with the parent's name.- Blocks need names.
- There is no
{% link %}tag.
- Template folders and passing data
- Default folder:
templates/. template_folder=replaces it.- Subfolders work:
"public/home.html". - Keyword arguments become template variables.
- Default folder:
Notes10 past questions in this topic
Week 2: HTML & CSSWeek 4: DatabasesAll MAD 1 topics and weeks