templates.rst
This file
Path
docs/tutorial/templates.rst
113 lines · 3.7 KiB
Last changed by
resolve merge conflictsKenneth Reitzbefore adoption31174fe
History of this fileTake it
1.. _tutorial-templates:23Step 7: The Templates4=====================56Now 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 by10default. 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.1718Create the follwing three HTML files and place them in the19:file:`templates` folder:2021.. _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 passed62in with the :func:`~flask.render_template` function. Notice that the form is63configured to submit to the `add_entry` view function and use ``POST`` as64HTTP method:6566.. 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 %}83 <li><h2>{{ entry.title }}</h2>{{ entry.text|safe }}</li>84 {% else %}85 <li><em>Unbelievable. No entries here so far</em></li>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`.