Member-only story
Python Web Development with Flask — Flash Messages
3 min readJan 31, 2021
Flask is a simple web framework written in Python.
In this article, we’ll look at how to develop simple Python web apps with Flask.
Message Flashing
We can send messages to a template that can be accessed with the next request.
For example, we can write:
app.py
from flask import Flask, render_template, redirect, url_for, flash, request
app = Flask(__name__)
app.secret_key = b'secret'@app.route('/')
def index():
return render_template('index.html')@app.route('/login', methods=['GET', 'POST'])
def login():
error = None
if request.method == 'POST':
flash('You were successfully logged in')
return redirect(url_for('index'))
return render_template('login.html', error=error)
templates/login.html
{% block body %}
<h1>Login</h1>
{% if error %}
<p class=error><strong>Error:</strong> {{ error }}
{% endif %}
<form method=post>
<dl>
<dt>Username:
<dd><input type=text name=username value="{{
request.form.username }}">
<dt>Password:
<dd><input type=password name=password>
</dl>
<p><input type=submit value=Login>
</form>
{% endblock %}