Skip to content
Advertisement

How can Jinja2 read a function without passing it explicitly?

In the flask:flashing documentation, I encounter the following situation.

from flask import Flask, flash, redirect, render_template, request, url_for

app = Flask(__name__)
app.secret_key = b'_5#y2L"F4Q8znxec]/'

@app.route('/')
def index():
    return render_template('index.html')

@app.route('/login', methods=['GET', 'POST'])
def login():
    error = None
    if request.method == 'POST':
        if request.form['username'] != 'admin' or request.form['password'] != 'secret':
            error = 'Invalid credentials'
        else:
            flash('You were successfully logged in')
            return redirect(url_for('index'))
    return render_template('login.html', error=error)

And here is the Jinja2 used HTML file.

<!doctype html>
<title>My Application</title>
{% with messages = get_flashed_messages() %}
  {% if messages %}
    <ul class=flashes>
    {% for message in messages %}
      <li>{{ message }}</li>
    {% endfor %}
    </ul>
  {% endif %}
{% endwith %}
{% block body %}{% endblock %}

So I wonder that even if there is no function passed in to the HTML file by using return statement in Python file, Jinja2 can read get_flashed_messages() function, which is a function under the flask module. How is this possible?

Advertisement

Answer

Messages to be flashed get appended to session, a global object that has access to along with what you return in your route functions.

From the github repo flask/src/flask/helpers.py:

from .globals import session

def flash(message: str, category: str = "message") -> None:
...
    flashes = session.get("_flashes", [])
    flashes.append((category, message))
    session["_flashes"] = flashes
...

‘s source code is available on github and it is well worth the exploration.

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement