Skip to content
Myr

templates.rst

This file

Path

docs/tutorial/templates.rst

111 lines · 3.6 KiB

Last changed by

Merge pull request #1418 from alexwlchan/masterKeyan Pishdadianbefore adoptiondac45f6

History of this file

Take it

The walk stopped after 200 commits: lines older than 0a7cc2b point at it.

Fix typoMarkus Unterwaditzerbefore adoption0a7cc2b

lines 1–5why, one step back
1.. _tutorial-templates:
23Step 6: The Templates4=====================5

Merge pull request #1418 from alexwlchan/masterKeyan Pishdadianbefore adoptiondac45f6

line 6why, one step back
6Now we should start working on the templates. If we were to request the URLs

Merge pull request #1277 from iKevinY/doc-fixesMarkus Unterwaditzerbefore adoption82d7c49

line 7why, one step back
7now, we would only get an exception that Flask cannot find the templates. The

Fix typoMarkus Unterwaditzerbefore adoption0a7cc2b

lines 8–16why, one step back
8templates are using `Jinja2`_ syntax and have autoescaping enabled by
9default. This means that unless you mark a value in the code with10:class:`~flask.Markup` or with the ``|safe`` filter in the template,11Jinja2 will ensure that special characters such as ``<`` or ``>`` are12escaped with their XML equivalents.1314We are also using template inheritance which makes it possible to reuse15the layout of the website in all pages.16

Merge pull request #1240 from defuz/docs-style-fix-2Markus Unterwaditzerbefore adoption6fcc2ae

line 17why, one step back
17Put the following templates into the :file:`templates` folder:

Fix typoMarkus Unterwaditzerbefore adoption0a7cc2b

lines 18–57why, one step back
18
19.. _Jinja2: http://jinja.pocoo.org/docs/templates2021layout.html22-----------2324This template contains the HTML skeleton, the header and a link to log in25(or log out if the user was already logged in). It also displays the26flashed messages if there are any. The ``{% block body %}`` block can be27replaced by a block of the same name (``body``) in a child template.2829The :class:`~flask.session` dict is available in the template as well and30you can use that to check if the user is logged in or not. Note that in31Jinja you can access missing attributes and items of objects / dicts which32makes the following code work, even if there is no ``'logged_in'`` key in33the session:3435.. sourcecode:: html+jinja3637 <!doctype html>38 <title>Flaskr</title>39 <link rel=stylesheet type=text/css href="{{ url_for('static', filename='style.css') }}">40 <div class=page>41 <h1>Flaskr</h1>42 <div class=metanav>43 {% if not session.logged_in %}44 <a href="{{ url_for('login') }}">log in</a>45 {% else %}46 <a href="{{ url_for('logout') }}">log out</a>47 {% endif %}48 </div>49 {% for message in get_flashed_messages() %}50 <div class=flash>{{ message }}</div>51 {% endfor %}52 {% block body %}{% endblock %}53 </div>5455show_entries.html56-----------------57

Merge pull request #1240 from defuz/docs-style-fix-2Markus Unterwaditzerbefore adoption6fcc2ae

lines 58–59why, one step back
58This template extends the :file:`layout.html` template from above to display the
59messages. Note that the ``for`` loop iterates over the messages we passed

Fix typoMarkus Unterwaditzerbefore adoption0a7cc2b

line 60why, one step back
60in with the :func:`~flask.render_template` function. We also tell the

Merge pull request #1240 from defuz/docs-style-fix-2Markus Unterwaditzerbefore adoption6fcc2ae

line 61why, one step back
61form to submit to your `add_entry` function and use ``POST`` as HTTP

Fix typoMarkus Unterwaditzerbefore adoption0a7cc2b

lines 62–90why, one step back
62method:
6364.. sourcecode:: html+jinja6566 {% extends "layout.html" %}67 {% block body %}68 {% if session.logged_in %}69 <form action="{{ url_for('add_entry') }}" method=post class=add-entry>70 <dl>71 <dt>Title:72 <dd><input type=text size=30 name=title>73 <dt>Text:74 <dd><textarea name=text rows=5 cols=40></textarea>75 <dd><input type=submit value=Share>76 </dl>77 </form>78 {% endif %}79 <ul class=entries>80 {% for entry in entries %}81 <li><h2>{{ entry.title }}</h2>{{ entry.text|safe }}82 {% else %}83 <li><em>Unbelievable. No entries here so far</em>84 {% endfor %}85 </ul>86 {% endblock %}8788login.html89----------90

Merge pull request #1277 from iKevinY/doc-fixesMarkus Unterwaditzerbefore adoption82d7c49

line 91why, one step back
91This is the login template, which basically just displays a form to allow

Fix typoMarkus Unterwaditzerbefore adoption0a7cc2b

lines 92–111why, one step back
92the user to login:
9394.. sourcecode:: html+jinja9596 {% extends "layout.html" %}97 {% block body %}98 <h2>Login</h2>99 {% if error %}<p class=error><strong>Error:</strong> {{ error }}{% endif %}100 <form action="{{ url_for('login') }}" method=post>101 <dl>102 <dt>Username:103 <dd><input type=text name=username>104 <dt>Password:105 <dd><input type=password name=password>106 <dd><input type=submit value=Login>107 </dl>108 </form>109 {% endblock %}110111Continue with :ref:`tutorial-css`.