Skip to content
Myr

templates.rst

This file

Path

docs/tutorial/templates.rst

111 lines · 3.6 KiB

Last changed by

Not known.

History of this file

Take it

The walk stopped after 200 commits: lines older than 60e143e point at it.

Documented change regarding the session cookie domain policy in the CHANGESArmin Ronacherbefore adoption60e143e

lines 1–111why, one step back
1.. _tutorial-templates:
23Step 6: The Templates4=====================56Now we should start working on the templates. If we request the URLs now7we would only get an exception that Flask cannot find the templates. The8templates are using `Jinja2`_ syntax and have autoescaping enabled by9default. 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.1617Put the following templates into the `templates` folder:1819.. _Jinja2: http://jinja.pocoo.org/2/documentation/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-----------------5758This template extends the `layout.html` template from above to display the59messages. Note that the `for` loop iterates over the messages we passed60in with the :func:`~flask.render_template` function. We also tell the61form to submit to your `add_entry` function and use `POST` as `HTTP`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----------9091Finally the login template which basically just displays a form to allow92the 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`.