//flash {% for message in get_flashed_messages() %} <div class="alert alert-warning"> <button type="button" class="close" data-dismiss="alert">×</button> {{ message }} </div> {% endfor %}
from flask import Flask, render_template, session, redirect, url_for, flash from flask_bootstrap import Bootstrap from flask_wtf import Form from wtforms import StringField,SubmitField from wtforms.validators import Required
class NameForm(Form): name=StringField("What is your name?",validators=[Required()]) submit=SubmitField("Submit")
app = Flask(name) bootstrap=Bootstrap(app) app.config['SECRET_KEY'] = 'hard to guess string'
@app.route('/',methods=['GET','POST']) def name(): form = NameForm() if form.validate_on_submit(): old_name = session.get('name') if old_name is not None and old_name != form.name.data: flash('Looks like you have changed your name!') session['name'] = form.name.data return redirect(url_for('name')) return render_template('name.html', form=form, name=session.get('name'))
if name == 'main': app.run()
name.html
{% extends "bootstrap/base.html" %} {% import "bootstrap/wtf.html" as wtf %} {% block content %} <div class="container"> {% for message in get_flashed_messages() %} <div class="alert alert-warning"> <button type="button" class="close" data-dismiss="alert">×</button> {{ message }} </div> {% endfor %} <div class="page-header"> <h1>Hello, {% if name %}{{ name }}{% else %}Stranger{% endif %}!</h1> </div> {{ wtf.quick_form(form) }} </div> {% endblock %}