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
The walk stopped after 200 commits: lines older than a51a167 point at it.
Merge pull request #1100 from thewhitetulip/patch-1Daniel Neuhäuserbefore adoptiona51a167
lines 1–2why, one step back1.. _tutorial-templates:2resolve merge conflictsKenneth Reitzbefore adoption31174fe
line 3why, one step back3Step 7: The TemplatesMerge pull request #1100 from thewhitetulip/patch-1Daniel Neuhäuserbefore adoptiona51a167
lines 4–5why, one step back4=====================5resolve merge conflictsKenneth Reitzbefore adoption31174fe
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 #1100 from thewhitetulip/patch-1Daniel Neuhäuserbefore adoptiona51a167
lines 10–17why, 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.17resolve merge conflictsKenneth Reitzbefore adoption31174fe
lines 18–19why, one step back18Create the follwing three HTML files and place them in the19:file:`templates` folder:Merge pull request #1100 from thewhitetulip/patch-1Daniel Neuhäuserbefore adoptiona51a167
lines 20–59why, one step back2021.. _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-----------------59Merge pull request #1240 from defuz/docs-style-fix-2Markus Unterwaditzerbefore adoption6fcc2ae
lines 60–61why, one step back60This template extends the :file:`layout.html` template from above to display the61messages. Note that the ``for`` loop iterates over the messages we passedresolve merge conflictsKenneth Reitzbefore adoption31174fe
lines 62–64why, one step back62in 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:Merge pull request #1100 from thewhitetulip/patch-1Daniel Neuhäuserbefore adoptiona51a167
lines 65–82why, one step back6566.. 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 %}resolve merge conflictsKenneth Reitzbefore adoption31174fe
line 83why, one step back83 <li><h2>{{ entry.title }}</h2>{{ entry.text|safe }}</li>Merge pull request #1100 from thewhitetulip/patch-1Daniel Neuhäuserbefore adoptiona51a167
line 84why, one step back84 {% else %}resolve merge conflictsKenneth Reitzbefore adoption31174fe
line 85why, one step back85 <li><em>Unbelievable. No entries here so far</em></li>Merge pull request #1100 from thewhitetulip/patch-1Daniel Neuhäuserbefore adoptiona51a167
lines 86–92why, one step back86 {% endfor %}87 </ul>88 {% endblock %}8990login.html91----------92Merge pull request #1277 from iKevinY/doc-fixesMarkus Unterwaditzerbefore adoption82d7c49
line 93why, one step back93This is the login template, which basically just displays a form to allowMerge pull request #1100 from thewhitetulip/patch-1Daniel Neuhäuserbefore adoptiona51a167
lines 94–113why, one step back94the 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`.