templates.rst
This file
Path
docs/tutorial/templates.rst
187 lines · 6.8 KiB
Last changed by
Merge pull request #3191 from jaydarius/masterDavid Lordbefore adoption49a0464
History of this fileTake it
The walk stopped after 200 commits: lines older than 4556ab2 point at it.
Merge pull request #2676 from davidism/tutorialDavid Lordbefore adoptionc3e2ff2
line 1why, one step back1.. currentmodule:: flaskMerge pull request #2548 from morenoh149/patch-1David Lordbefore adoption4556ab2
line 2why, one step back2Merge pull request #2676 from davidism/tutorialDavid Lordbefore adoptionc3e2ff2
lines 3–4why, one step back3Templates4=========Merge pull request #2548 from morenoh149/patch-1David Lordbefore adoption4556ab2
line 5why, one step back5Merge pull request #2676 from davidism/tutorialDavid Lordbefore adoptionc3e2ff2
lines 6–11why, one step back6You've written the authentication views for your application, but if7you're running the server and try to go to any of the URLs, you'll see a8``TemplateNotFound`` error. That's because the views are calling9:func:`render_template`, but you haven't written the templates yet.10The template files will be stored in the ``templates`` directory inside11the ``flaskr`` package.Merge pull request #2548 from morenoh149/patch-1David Lordbefore adoption4556ab2
line 12why, one step back12Merge pull request #2676 from davidism/tutorialDavid Lordbefore adoptionc3e2ff2
lines 13–16why, one step back13Templates are files that contain static data as well as placeholders14for dynamic data. A template is rendered with specific data to produce a15final document. Flask uses the `Jinja`_ template library to render16templates.Merge pull request #2548 from morenoh149/patch-1David Lordbefore adoption4556ab2
line 17why, one step back17Merge pull request #2676 from davidism/tutorialDavid Lordbefore adoptionc3e2ff2
lines 18–24why, one step back18In your application, you will use templates to render `HTML`_ which19will display in the user's browser. In Flask, Jinja is configured to20*autoescape* any data that is rendered in HTML templates. This means21that it's safe to render user input; any characters they've entered that22could mess with the HTML, such as ``<`` and ``>`` will be *escaped* with23*safe* values that look the same in the browser but don't cause unwanted24effects.Merge pull request #2548 from morenoh149/patch-1David Lordbefore adoption4556ab2
line 25why, one step back25Merge pull request #2676 from davidism/tutorialDavid Lordbefore adoptionc3e2ff2
lines 26–32why, one step back26Jinja looks and behaves mostly like Python. Special delimiters are used27to distinguish Jinja syntax from the static data in the template.28Anything between ``{{`` and ``}}`` is an expression that will be output29to the final document. ``{%`` and ``%}`` denotes a control flow30statement like ``if`` and ``for``. Unlike Python, blocks are denoted31by start and end tags rather than indentation since static text within32a block could change indentation.Merge pull request #2548 from morenoh149/patch-1David Lordbefore adoption4556ab2
line 33why, one step back33Merge pull request #2676 from davidism/tutorialDavid Lordbefore adoptionc3e2ff2
lines 34–35why, one step back34.. _Jinja: http://jinja.pocoo.org/docs/templates/35.. _HTML: https://developer.mozilla.org/docs/Web/HTMLMerge pull request #2548 from morenoh149/patch-1David Lordbefore adoption4556ab2
lines 36–37why, one step back3637Merge pull request #2676 from davidism/tutorialDavid Lordbefore adoptionc3e2ff2
lines 38–39why, one step back38The Base Layout39---------------Merge pull request #2548 from morenoh149/patch-1David Lordbefore adoption4556ab2
line 40why, one step back40Merge pull request #2676 from davidism/tutorialDavid Lordbefore adoptionc3e2ff2
lines 41–44why, one step back41Each page in the application will have the same basic layout around a42different body. Instead of writing the entire HTML structure in each43template, each template will *extend* a base template and override44specific sections.Merge pull request #2548 from morenoh149/patch-1David Lordbefore adoption4556ab2
line 45why, one step back45Merge pull request #2676 from davidism/tutorialDavid Lordbefore adoptionc3e2ff2
lines 46–48why, one step back46.. code-block:: html+jinja47 :caption: ``flaskr/templates/base.html``48Merge pull request #2548 from morenoh149/patch-1David Lordbefore adoption4556ab2
line 49why, one step back49 <!doctype html>Merge pull request #2676 from davidism/tutorialDavid Lordbefore adoptionc3e2ff2
lines 50–52why, one step back50 <title>{% block title %}{% endblock %} - Flaskr</title>51 <link rel="stylesheet" href="{{ url_for('static', filename='style.css') }}">52 <nav>Merge pull request #2548 from morenoh149/patch-1David Lordbefore adoption4556ab2
line 53why, one step back53 <h1>Flaskr</h1>Merge pull request #2676 from davidism/tutorialDavid Lordbefore adoptionc3e2ff2
lines 54–67why, one step back54 <ul>55 {% if g.user %}56 <li><span>{{ g.user['username'] }}</span>57 <li><a href="{{ url_for('auth.logout') }}">Log Out</a>58 {% else %}59 <li><a href="{{ url_for('auth.register') }}">Register</a>60 <li><a href="{{ url_for('auth.login') }}">Log In</a>61 {% endif %}62 </ul>63 </nav>64 <section class="content">65 <header>66 {% block header %}{% endblock %}67 </header>Merge pull request #2548 from morenoh149/patch-1David Lordbefore adoption4556ab2
line 68why, one step back68 {% for message in get_flashed_messages() %}Merge pull request #2676 from davidism/tutorialDavid Lordbefore adoptionc3e2ff2
line 69why, one step back69 <div class="flash">{{ message }}</div>Merge pull request #2548 from morenoh149/patch-1David Lordbefore adoption4556ab2
line 70why, one step back70 {% endfor %}Merge pull request #2676 from davidism/tutorialDavid Lordbefore adoptionc3e2ff2
lines 71–72why, one step back71 {% block content %}{% endblock %}72 </section>Merge pull request #2548 from morenoh149/patch-1David Lordbefore adoption4556ab2
line 73why, one step back73Merge pull request #2676 from davidism/tutorialDavid Lordbefore adoptionc3e2ff2
lines 74–75why, one step back74:data:`g` is automatically available in templates. Based on if75``g.user`` is set (from ``load_logged_in_user``), either the usernameMerge pull request #3191 from jaydarius/masterDavid Lordbefore adoption49a0464
line 76why, one step back76and a log out link are displayed, or links to register and log inMerge pull request #2676 from davidism/tutorialDavid Lordbefore adoptionc3e2ff2
lines 77–78why, one step back77are displayed. :func:`url_for` is also automatically available, and is78used to generate URLs to views instead of writing them out manually.Merge pull request #2548 from morenoh149/patch-1David Lordbefore adoption4556ab2
line 79why, one step back79Merge pull request #2676 from davidism/tutorialDavid Lordbefore adoptionc3e2ff2
lines 80–83why, one step back80After the page title, and before the content, the template loops over81each message returned by :func:`get_flashed_messages`. You used82:func:`flash` in the views to show error messages, and this is the code83that will display them.Merge pull request #2548 from morenoh149/patch-1David Lordbefore adoption4556ab2
line 84why, one step back84Merge pull request #2676 from davidism/tutorialDavid Lordbefore adoptionc3e2ff2
lines 85–86why, one step back85There are three blocks defined here that will be overridden in the other86templates:Merge pull request #2548 from morenoh149/patch-1David Lordbefore adoption4556ab2
line 87why, one step back87Merge pull request #2676 from davidism/tutorialDavid Lordbefore adoptionc3e2ff2
lines 88–111why, one step back88#. ``{% block title %}`` will change the title displayed in the89 browser's tab and window title.9091#. ``{% block header %}`` is similar to ``title`` but will change the92 title displayed on the page.9394#. ``{% block content %}`` is where the content of each page goes, such95 as the login form or a blog post.9697The base template is directly in the ``templates`` directory. To keep98the others organized, the templates for a blueprint will be placed in a99directory with the same name as the blueprint.100101102Register103--------104105.. code-block:: html+jinja106 :caption: ``flaskr/templates/auth/register.html``107108 {% extends 'base.html' %}109110 {% block header %}111 <h1>{% block title %}Register{% endblock %}</h1>Merge pull request #2548 from morenoh149/patch-1David Lordbefore adoption4556ab2
lines 112–113why, one step back112 {% endblock %}113Merge pull request #2676 from davidism/tutorialDavid Lordbefore adoptionc3e2ff2
lines 114–122why, one step back114 {% block content %}115 <form method="post">116 <label for="username">Username</label>117 <input name="username" id="username" required>118 <label for="password">Password</label>119 <input type="password" name="password" id="password" required>120 <input type="submit" value="Register">121 </form>122 {% endblock %}Merge pull request #2548 from morenoh149/patch-1David Lordbefore adoption4556ab2
line 123why, one step back123Merge pull request #2676 from davidism/tutorialDavid Lordbefore adoptionc3e2ff2
lines 124–127why, one step back124``{% extends 'base.html' %}`` tells Jinja that this template should125replace the blocks from the base template. All the rendered content must126appear inside ``{% block %}`` tags that override blocks from the base127template.Merge pull request #2548 from morenoh149/patch-1David Lordbefore adoption4556ab2
line 128why, one step back128Merge pull request #2676 from davidism/tutorialDavid Lordbefore adoptionc3e2ff2
lines 129–132why, one step back129A useful pattern used here is to place ``{% block title %}`` inside130``{% block header %}``. This will set the title block and then output131the value of it into the header block, so that both the window and page132share the same title without writing it twice.Merge pull request #2548 from morenoh149/patch-1David Lordbefore adoption4556ab2
line 133why, one step back133Merge pull request #2676 from davidism/tutorialDavid Lordbefore adoptionc3e2ff2
lines 134–164why, one step back134The ``input`` tags are using the ``required`` attribute here. This tells135the browser not to submit the form until those fields are filled in. If136the user is using an older browser that doesn't support that attribute,137or if they are using something besides a browser to make requests, you138still want to validate the data in the Flask view. It's important to139always fully validate the data on the server, even if the client does140some validation as well.141142143Log In144------145146This is identical to the register template except for the title and147submit button.148149.. code-block:: html+jinja150 :caption: ``flaskr/templates/auth/login.html``151152 {% extends 'base.html' %}153154 {% block header %}155 <h1>{% block title %}Log In{% endblock %}</h1>156 {% endblock %}157158 {% block content %}159 <form method="post">160 <label for="username">Username</label>161 <input name="username" id="username" required>162 <label for="password">Password</label>163 <input type="password" name="password" id="password" required>164 <input type="submit" value="Log In">Merge pull request #2548 from morenoh149/patch-1David Lordbefore adoption4556ab2
lines 165–167why, one step back165 </form>166 {% endblock %}167Merge pull request #2676 from davidism/tutorialDavid Lordbefore adoptionc3e2ff2
lines 168–187why, one step back168169Register A User170---------------171172Now that the authentication templates are written, you can register a173user. Make sure the server is still running (``flask run`` if it's not),174then go to http://127.0.0.1:5000/auth/register.175176Try clicking the "Register" button without filling out the form and see177that the browser shows an error message. Try removing the ``required``178attributes from the ``register.html`` template and click "Register"179again. Instead of the browser showing an error, the page will reload and180the error from :func:`flash` in the view will be shown.181182Fill out a username and password and you'll be redirected to the login183page. Try entering an incorrect username, or the correct username and184incorrect password. If you log in you'll get an error because there's185no ``index`` view to redirect to yet.186187Continue to :doc:`static`.