from flask import Flask, render_template, session, redirect, url_for @app.route('/', methods=['GET', 'POST']) defindex(): form = NameForm() if form.validate_on_submit(): session['name'] = form.name.data return redirect(url_for('index')) return render_template('index.html', form=form, name=session.get('name'))
Flash消息
导入:from import flash
使用:flash(string)and{ % for message in get_flashed_messages() %}
1 2 3 4 5 6 7 8 9 10 11
from flask import Flask, render_template, session, redirect, url_for, flash @app.route('/', methods=['GET', 'POST']) defindex(): form = NameForm() if form.validate_on_submit(): old_name = session.get('name') if old_name isnotNoneand old_name != form.name.data: flash('Looks like you have changed your name!') session['name'] = form.name.data return redirect(url_for('index')) return render_template('index.html', form = form, name = session.get('name'))