1{% with messages = get_flashed_messages(with_categories=true) %}
2 {% if messages %}
3 {% for category, message in messages %}
4 <div class="alert alert-{{ category }} alert-dismissible fade show" role="alert">
5 <span>{{ message }}</span>
6 <button type="button" class="close" data-dismiss="alert" aria-label="Close">
7 <span aria-hidden="true">×</span>
8 </button>
9 </div>
10 {% endfor %}
11 {% endif %}
12{% endwith %}
13
1from flask import Flask, flash, redirect, render_template, \
2 request, url_for
3
4app = Flask(__name__)
5app.secret_key = b'_5#y2L"F4Q8z\n\xec]/'
6
7@app.route('/')
8def index():
9 return render_template('index.html')
10
11@app.route('/login', methods=['GET', 'POST'])
12def login():
13 error = None
14 if request.method == 'POST':
15 if request.form['username'] != 'admin' or \
16 request.form['password'] != 'secret':
17 error = 'Invalid credentials'
18 else:
19 flash('You were successfully logged in')
20 return redirect(url_for('index'))
21 return render_template('login.html', error=error)
22