Skip to content
Myr

templates.rst

This file

Path

docs/tutorial/templates.rst

187 lines · 6.8 KiB

Last changed by

Merge pull request #3191 from jaydarius/masterDavid Lordbefore adoption49a0464

History of this file

Take it

The walk stopped after 200 commits: lines older than 54f3d21 point at it.

Merge pull request #2676 from davidism/tutorialDavid Lordbefore adoptionc3e2ff2

line 1why, one step back
1.. currentmodule:: flask

Merge pull request #2541 from nsiregar/#2535_update_pycharm_instructionDavid Lordbefore adoption54f3d21

line 2why, one step back
2

Merge pull request #2676 from davidism/tutorialDavid Lordbefore adoptionc3e2ff2

lines 3–4why, one step back
3Templates
4=========

Merge pull request #2541 from nsiregar/#2535_update_pycharm_instructionDavid Lordbefore adoption54f3d21

line 5why, one step back
5

Merge pull request #2676 from davidism/tutorialDavid Lordbefore adoptionc3e2ff2

lines 6–11why, one step back
6You've written the authentication views for your application, but if
7you're running the server and try to go to any of the URLs, you'll see a8``TemplateNotFound`` error. That's because the views are calling9:func:`render_template`, but you haven't written the templates yet.10The template files will be stored in the ``templates`` directory inside11the ``flaskr`` package.

Merge pull request #2541 from nsiregar/#2535_update_pycharm_instructionDavid Lordbefore adoption54f3d21

line 12why, one step back
12

Merge pull request #2676 from davidism/tutorialDavid Lordbefore adoptionc3e2ff2

lines 13–16why, one step back
13Templates are files that contain static data as well as placeholders
14for dynamic data. A template is rendered with specific data to produce a15final document. Flask uses the `Jinja`_ template library to render16templates.

Merge pull request #2541 from nsiregar/#2535_update_pycharm_instructionDavid Lordbefore adoption54f3d21

line 17why, one step back
17

Merge pull request #2676 from davidism/tutorialDavid Lordbefore adoptionc3e2ff2

lines 18–24why, one step back
18In your application, you will use templates to render `HTML`_ which
19will display in the user's browser. In Flask, Jinja is configured to20*autoescape* any data that is rendered in HTML templates. This means21that it's safe to render user input; any characters they've entered that22could mess with the HTML, such as ``<`` and ``>`` will be *escaped* with23*safe* values that look the same in the browser but don't cause unwanted24effects.

Merge pull request #2541 from nsiregar/#2535_update_pycharm_instructionDavid Lordbefore adoption54f3d21

line 25why, one step back
25

Merge pull request #2676 from davidism/tutorialDavid Lordbefore adoptionc3e2ff2

lines 26–32why, one step back
26Jinja looks and behaves mostly like Python. Special delimiters are used
27to distinguish Jinja syntax from the static data in the template.28Anything between ``{{`` and ``}}`` is an expression that will be output29to the final document. ``{%`` and ``%}`` denotes a control flow30statement like ``if`` and ``for``. Unlike Python, blocks are denoted31by start and end tags rather than indentation since static text within32a block could change indentation.

Merge pull request #2541 from nsiregar/#2535_update_pycharm_instructionDavid Lordbefore adoption54f3d21

line 33why, one step back
33

Merge pull request #2676 from davidism/tutorialDavid Lordbefore adoptionc3e2ff2

lines 34–35why, one step back
34.. _Jinja: http://jinja.pocoo.org/docs/templates/
35.. _HTML: https://developer.mozilla.org/docs/Web/HTML

Merge pull request #2541 from nsiregar/#2535_update_pycharm_instructionDavid Lordbefore adoption54f3d21

lines 36–37why, one step back
36
37

Merge pull request #2676 from davidism/tutorialDavid Lordbefore adoptionc3e2ff2

lines 38–39why, one step back
38The Base Layout
39---------------

Merge pull request #2541 from nsiregar/#2535_update_pycharm_instructionDavid Lordbefore adoption54f3d21

line 40why, one step back
40

Merge pull request #2676 from davidism/tutorialDavid Lordbefore adoptionc3e2ff2

lines 41–44why, one step back
41Each page in the application will have the same basic layout around a
42different body. Instead of writing the entire HTML structure in each43template, each template will *extend* a base template and override44specific sections.

Merge pull request #2541 from nsiregar/#2535_update_pycharm_instructionDavid Lordbefore adoption54f3d21

line 45why, one step back
45

Merge pull request #2676 from davidism/tutorialDavid Lordbefore adoptionc3e2ff2

lines 46–48why, one step back
46.. code-block:: html+jinja
47 :caption: ``flaskr/templates/base.html``48

Merge pull request #2541 from nsiregar/#2535_update_pycharm_instructionDavid Lordbefore adoption54f3d21

line 49why, one step back
49 <!doctype html>

Merge pull request #2676 from davidism/tutorialDavid Lordbefore adoptionc3e2ff2

lines 50–52why, one step back
50 <title>{% block title %}{% endblock %} - Flaskr</title>
51 <link rel="stylesheet" href="{{ url_for('static', filename='style.css') }}">52 <nav>

Merge pull request #2541 from nsiregar/#2535_update_pycharm_instructionDavid Lordbefore adoption54f3d21

line 53why, one step back
53 <h1>Flaskr</h1>

Merge pull request #2676 from davidism/tutorialDavid Lordbefore adoptionc3e2ff2

lines 54–67why, one step back
54 <ul>
55 {% if g.user %}56 <li><span>{{ g.user['username'] }}</span>57 <li><a href="{{ url_for('auth.logout') }}">Log Out</a>58 {% else %}59 <li><a href="{{ url_for('auth.register') }}">Register</a>60 <li><a href="{{ url_for('auth.login') }}">Log In</a>61 {% endif %}62 </ul>63 </nav>64 <section class="content">65 <header>66 {% block header %}{% endblock %}67 </header>

Merge pull request #2541 from nsiregar/#2535_update_pycharm_instructionDavid Lordbefore adoption54f3d21

line 68why, one step back
68 {% for message in get_flashed_messages() %}

Merge pull request #2676 from davidism/tutorialDavid Lordbefore adoptionc3e2ff2

line 69why, one step back
69 <div class="flash">{{ message }}</div>

Merge pull request #2541 from nsiregar/#2535_update_pycharm_instructionDavid Lordbefore adoption54f3d21

line 70why, one step back
70 {% endfor %}

Merge pull request #2676 from davidism/tutorialDavid Lordbefore adoptionc3e2ff2

lines 71–72why, one step back
71 {% block content %}{% endblock %}
72 </section>

Merge pull request #2541 from nsiregar/#2535_update_pycharm_instructionDavid Lordbefore adoption54f3d21

line 73why, one step back
73

Merge pull request #2676 from davidism/tutorialDavid Lordbefore adoptionc3e2ff2

lines 74–75why, one step back
74:data:`g` is automatically available in templates. Based on if
75``g.user`` is set (from ``load_logged_in_user``), either the username

Merge pull request #3191 from jaydarius/masterDavid Lordbefore adoption49a0464

line 76why, one step back
76and a log out link are displayed, or links to register and log in

Merge pull request #2676 from davidism/tutorialDavid Lordbefore adoptionc3e2ff2

lines 77–78why, one step back
77are displayed. :func:`url_for` is also automatically available, and is
78used to generate URLs to views instead of writing them out manually.

Merge pull request #2541 from nsiregar/#2535_update_pycharm_instructionDavid Lordbefore adoption54f3d21

line 79why, one step back
79

Merge pull request #2676 from davidism/tutorialDavid Lordbefore adoptionc3e2ff2

lines 80–83why, one step back
80After the page title, and before the content, the template loops over
81each message returned by :func:`get_flashed_messages`. You used82:func:`flash` in the views to show error messages, and this is the code83that will display them.

Merge pull request #2541 from nsiregar/#2535_update_pycharm_instructionDavid Lordbefore adoption54f3d21

line 84why, one step back
84

Merge pull request #2676 from davidism/tutorialDavid Lordbefore adoptionc3e2ff2

lines 85–86why, one step back
85There are three blocks defined here that will be overridden in the other
86templates:

Merge pull request #2541 from nsiregar/#2535_update_pycharm_instructionDavid Lordbefore adoption54f3d21

line 87why, one step back
87

Merge pull request #2676 from davidism/tutorialDavid Lordbefore adoptionc3e2ff2

lines 88–111why, one step back
88#. ``{% block title %}`` will change the title displayed in the
89 browser's tab and window title.9091#. ``{% block header %}`` is similar to ``title`` but will change the92 title displayed on the page.9394#. ``{% block content %}`` is where the content of each page goes, such95 as the login form or a blog post.9697The base template is directly in the ``templates`` directory. To keep98the others organized, the templates for a blueprint will be placed in a99directory with the same name as the blueprint.100101102Register103--------104105.. code-block:: html+jinja106 :caption: ``flaskr/templates/auth/register.html``107108 {% extends 'base.html' %}109110 {% block header %}111 <h1>{% block title %}Register{% endblock %}</h1>

Merge pull request #2541 from nsiregar/#2535_update_pycharm_instructionDavid Lordbefore adoption54f3d21

lines 112–113why, one step back
112 {% endblock %}
113

Merge pull request #2676 from davidism/tutorialDavid Lordbefore adoptionc3e2ff2

lines 114–122why, one step back
114 {% block content %}
115 <form method="post">116 <label for="username">Username</label>117 <input name="username" id="username" required>118 <label for="password">Password</label>119 <input type="password" name="password" id="password" required>120 <input type="submit" value="Register">121 </form>122 {% endblock %}

Merge pull request #2541 from nsiregar/#2535_update_pycharm_instructionDavid Lordbefore adoption54f3d21

line 123why, one step back
123

Merge pull request #2676 from davidism/tutorialDavid Lordbefore adoptionc3e2ff2

lines 124–127why, one step back
124``{% extends 'base.html' %}`` tells Jinja that this template should
125replace the blocks from the base template. All the rendered content must126appear inside ``{% block %}`` tags that override blocks from the base127template.

Merge pull request #2541 from nsiregar/#2535_update_pycharm_instructionDavid Lordbefore adoption54f3d21

line 128why, one step back
128

Merge pull request #2676 from davidism/tutorialDavid Lordbefore adoptionc3e2ff2

lines 129–132why, one step back
129A useful pattern used here is to place ``{% block title %}`` inside
130``{% block header %}``. This will set the title block and then output131the value of it into the header block, so that both the window and page132share the same title without writing it twice.

Merge pull request #2541 from nsiregar/#2535_update_pycharm_instructionDavid Lordbefore adoption54f3d21

line 133why, one step back
133

Merge pull request #2676 from davidism/tutorialDavid Lordbefore adoptionc3e2ff2

lines 134–164why, one step back
134The ``input`` tags are using the ``required`` attribute here. This tells
135the browser not to submit the form until those fields are filled in. If136the user is using an older browser that doesn't support that attribute,137or if they are using something besides a browser to make requests, you138still want to validate the data in the Flask view. It's important to139always fully validate the data on the server, even if the client does140some validation as well.141142143Log In144------145146This is identical to the register template except for the title and147submit button.148149.. code-block:: html+jinja150 :caption: ``flaskr/templates/auth/login.html``151152 {% extends 'base.html' %}153154 {% block header %}155 <h1>{% block title %}Log In{% endblock %}</h1>156 {% endblock %}157158 {% block content %}159 <form method="post">160 <label for="username">Username</label>161 <input name="username" id="username" required>162 <label for="password">Password</label>163 <input type="password" name="password" id="password" required>164 <input type="submit" value="Log In">

Merge pull request #2541 from nsiregar/#2535_update_pycharm_instructionDavid Lordbefore adoption54f3d21

lines 165–167why, one step back
165 </form>
166 {% endblock %}167

Merge pull request #2676 from davidism/tutorialDavid Lordbefore adoptionc3e2ff2

lines 168–187why, one step back
168
169Register A User170---------------171172Now that the authentication templates are written, you can register a173user. Make sure the server is still running (``flask run`` if it's not),174then go to http://127.0.0.1:5000/auth/register.175176Try clicking the "Register" button without filling out the form and see177that the browser shows an error message. Try removing the ``required``178attributes from the ``register.html`` template and click "Register"179again. Instead of the browser showing an error, the page will reload and180the error from :func:`flash` in the view will be shown.181182Fill out a username and password and you'll be redirected to the login183page. Try entering an incorrect username, or the correct username and184incorrect password. If you log in you'll get an error because there's185no ``index`` view to redirect to yet.186187Continue to :doc:`static`.