Skip to content
Myr

templates.rst

This file

Path

docs/tutorial/templates.rst

113 lines · 3.7 KiB

Last changed by

Merge branch 'master' into masterKenneth Reitzbefore adoptiond911c89

History of this file

Take it

The walk stopped after 200 commits: lines older than 4542bac point at it.

Merge pull request #1733 from welchbj/patch-1Markus Unterwaditzerbefore adoption4542bac

lines 1–2why, one step back
1.. _tutorial-templates:
2

Merge branch '0.11-maintenance'Markus Unterwaditzerbefore adoption7132feb

line 3why, one step back
3Step 7: The Templates

Merge pull request #1733 from welchbj/patch-1Markus Unterwaditzerbefore adoption4542bac

lines 4–5why, one step back
4=====================
5

Merge branch '0.11-maintenance'David Lordbefore adoption633b13e

lines 6–9why, one step back
6Now it is time to start working on the templates. As you may have
7noticed, if you make requests with the app running, you will get8an exception that Flask cannot find the templates. The templates9are using `Jinja2`_ syntax and have autoescaping enabled by

Merge pull request #1733 from welchbj/patch-1Markus Unterwaditzerbefore adoption4542bac

lines 10–17why, one step back
10default. This means that unless you mark a value in the code with
11:class:`~flask.Markup` or with the ``|safe`` filter in the template,12Jinja2 will ensure that special characters such as ``<`` or ``>`` are13escaped with their XML equivalents.1415We are also using template inheritance which makes it possible to reuse16the layout of the website in all pages.17

Merge branch 'master' into masterKenneth Reitzbefore adoptiond911c89

lines 18–19why, one step back
18Create the follwing three HTML files and place them in the
19:file:`templates` folder:

Merge pull request #1733 from welchbj/patch-1Markus Unterwaditzerbefore adoption4542bac

lines 20–61why, one step back
20
21.. _Jinja2: http://jinja.pocoo.org/docs/templates2223layout.html24-----------2526This template contains the HTML skeleton, the header and a link to log in27(or log out if the user was already logged in). It also displays the28flashed messages if there are any. The ``{% block body %}`` block can be29replaced by a block of the same name (``body``) in a child template.3031The :class:`~flask.session` dict is available in the template as well and32you can use that to check if the user is logged in or not. Note that in33Jinja you can access missing attributes and items of objects / dicts which34makes the following code work, even if there is no ``'logged_in'`` key in35the session:3637.. sourcecode:: html+jinja3839 <!doctype html>40 <title>Flaskr</title>41 <link rel=stylesheet type=text/css href="{{ url_for('static', filename='style.css') }}">42 <div class=page>43 <h1>Flaskr</h1>44 <div class=metanav>45 {% if not session.logged_in %}46 <a href="{{ url_for('login') }}">log in</a>47 {% else %}48 <a href="{{ url_for('logout') }}">log out</a>49 {% endif %}50 </div>51 {% for message in get_flashed_messages() %}52 <div class=flash>{{ message }}</div>53 {% endfor %}54 {% block body %}{% endblock %}55 </div>5657show_entries.html58-----------------5960This template extends the :file:`layout.html` template from above to display the61messages. Note that the ``for`` loop iterates over the messages we passed

Merge branch '0.11-maintenance'David Lordbefore adoption633b13e

line 62why, one step back
62in with the :func:`~flask.render_template` function. Notice that the form is

Fix typo in docs/tutorial/templates.rst (#2186)Timothy John Perisho Ecclestonbefore adoptiondc5f48f

line 63why, one step back
63configured to submit to the `add_entry` view function and use ``POST`` as

Merge branch '0.11-maintenance'David Lordbefore adoption633b13e

line 64why, one step back
64HTTP method:

Merge pull request #1733 from welchbj/patch-1Markus Unterwaditzerbefore adoption4542bac

lines 65–82why, one step back
65
66.. sourcecode:: html+jinja6768 {% extends "layout.html" %}69 {% block body %}70 {% if session.logged_in %}71 <form action="{{ url_for('add_entry') }}" method=post class=add-entry>72 <dl>73 <dt>Title:74 <dd><input type=text size=30 name=title>75 <dt>Text:76 <dd><textarea name=text rows=5 cols=40></textarea>77 <dd><input type=submit value=Share>78 </dl>79 </form>80 {% endif %}81 <ul class=entries>82 {% for entry in entries %}

Merge branch 'master' into masterKenneth Reitzbefore adoptiond911c89

line 83why, one step back
83 <li><h2>{{ entry.title }}</h2>{{ entry.text|safe }}</li>

Merge pull request #1733 from welchbj/patch-1Markus Unterwaditzerbefore adoption4542bac

line 84why, one step back
84 {% else %}

Merge branch 'master' into masterKenneth Reitzbefore adoptiond911c89

line 85why, one step back
85 <li><em>Unbelievable. No entries here so far</em></li>

Merge pull request #1733 from welchbj/patch-1Markus Unterwaditzerbefore adoption4542bac

lines 86–113why, one step back
86 {% endfor %}
87 </ul>88 {% endblock %}8990login.html91----------9293This is the login template, which basically just displays a form to allow94the user to login:9596.. sourcecode:: html+jinja9798 {% extends "layout.html" %}99 {% block body %}100 <h2>Login</h2>101 {% if error %}<p class=error><strong>Error:</strong> {{ error }}{% endif %}102 <form action="{{ url_for('login') }}" method=post>103 <dl>104 <dt>Username:105 <dd><input type=text name=username>106 <dt>Password:107 <dd><input type=password name=password>108 <dd><input type=submit value=Login>109 </dl>110 </form>111 {% endblock %}112113Continue with :ref:`tutorial-css`.