templates.rst
This file
Path
docs/tutorial/templates.rst
112 lines · 3.6 KiB
Last changed by
Merge branch '0.11-maintenance'Markus Unterwaditzerbefore adoption7132feb
History of this fileTake it
The walk stopped after 200 commits: lines older than 7df543d point at it.
Merge pull request #1703 from jeffwidman/masterDavid Lordbefore adoption7df543d
lines 1–2why, one step back1.. _tutorial-templates:2Merge branch '0.11-maintenance'Markus Unterwaditzerbefore adoption7132feb
line 3why, one step back3Step 7: The TemplatesMerge pull request #1703 from jeffwidman/masterDavid Lordbefore adoption7df543d
lines 4–5why, one step back4=====================5Merge branch '0.11-maintenance'David Lordbefore adoption633b13e
lines 6–9why, one step back6Now it is time to start working on the templates. As you may have7noticed, 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 byMerge pull request #1703 from jeffwidman/masterDavid Lordbefore adoption7df543d
lines 10–60why, one step back10default. This means that unless you mark a value in the code with11: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.1718Put the following templates into the :file:`templates` folder:1920.. _Jinja2: http://jinja.pocoo.org/docs/templates2122layout.html23-----------2425This template contains the HTML skeleton, the header and a link to log in26(or log out if the user was already logged in). It also displays the27flashed messages if there are any. The ``{% block body %}`` block can be28replaced by a block of the same name (``body``) in a child template.2930The :class:`~flask.session` dict is available in the template as well and31you can use that to check if the user is logged in or not. Note that in32Jinja you can access missing attributes and items of objects / dicts which33makes the following code work, even if there is no ``'logged_in'`` key in34the session:3536.. sourcecode:: html+jinja3738 <!doctype html>39 <title>Flaskr</title>40 <link rel=stylesheet type=text/css href="{{ url_for('static', filename='style.css') }}">41 <div class=page>42 <h1>Flaskr</h1>43 <div class=metanav>44 {% if not session.logged_in %}45 <a href="{{ url_for('login') }}">log in</a>46 {% else %}47 <a href="{{ url_for('logout') }}">log out</a>48 {% endif %}49 </div>50 {% for message in get_flashed_messages() %}51 <div class=flash>{{ message }}</div>52 {% endfor %}53 {% block body %}{% endblock %}54 </div>5556show_entries.html57-----------------5859This template extends the :file:`layout.html` template from above to display the60messages. Note that the ``for`` loop iterates over the messages we passedMerge branch '0.11-maintenance'David Lordbefore adoption633b13e
lines 61–63why, one step back61in with the :func:`~flask.render_template` function. Notice that the form is62configured to to submit to the `add_entry` view function and use ``POST`` as63HTTP method:Merge pull request #1703 from jeffwidman/masterDavid Lordbefore adoption7df543d
lines 64–112why, one step back6465.. sourcecode:: html+jinja6667 {% extends "layout.html" %}68 {% block body %}69 {% if session.logged_in %}70 <form action="{{ url_for('add_entry') }}" method=post class=add-entry>71 <dl>72 <dt>Title:73 <dd><input type=text size=30 name=title>74 <dt>Text:75 <dd><textarea name=text rows=5 cols=40></textarea>76 <dd><input type=submit value=Share>77 </dl>78 </form>79 {% endif %}80 <ul class=entries>81 {% for entry in entries %}82 <li><h2>{{ entry.title }}</h2>{{ entry.text|safe }}83 {% else %}84 <li><em>Unbelievable. No entries here so far</em>85 {% endfor %}86 </ul>87 {% endblock %}8889login.html90----------9192This is the login template, which basically just displays a form to allow93the user to login:9495.. sourcecode:: html+jinja9697 {% extends "layout.html" %}98 {% block body %}99 <h2>Login</h2>100 {% if error %}<p class=error><strong>Error:</strong> {{ error }}{% endif %}101 <form action="{{ url_for('login') }}" method=post>102 <dl>103 <dt>Username:104 <dd><input type=text name=username>105 <dt>Password:106 <dd><input type=password name=password>107 <dd><input type=submit value=Login>108 </dl>109 </form>110 {% endblock %}111112Continue with :ref:`tutorial-css`.