Browse Source

Chapter 4: Message flashing (4c)

master
T. Meissner 6 years ago
parent
commit
c3385ab430
2 changed files with 10 additions and 1 deletions
  1. +4
    -1
      hello.py
  2. +6
    -0
      templates/base.html

+ 4
- 1
hello.py View File

@ -1,4 +1,4 @@
from flask import Flask, render_template, session, redirect, url_for
from flask import Flask, render_template, session, redirect, url_for, flash
from flask_bootstrap import Bootstrap from flask_bootstrap import Bootstrap
from flask_moment import Moment from flask_moment import Moment
from flask_wtf import FlaskForm from flask_wtf import FlaskForm
@ -32,6 +32,9 @@ def internal_server_error(e):
def index(): def index():
form = NameForm() form = NameForm()
if form.validate_on_submit(): if form.validate_on_submit():
old_name = session.get('name')
if old_name is not None and old_name != form.name.data:
flash('Loks like you have changed your name!')
session['name'] = form.name.data session['name'] = form.name.data
return redirect(url_for('index')) return redirect(url_for('index'))
return render_template('index.html', form=form, name=session.get('name')) return render_template('index.html', form=form, name=session.get('name'))

+ 6
- 0
templates/base.html View File

@ -32,6 +32,12 @@
{% block content %} {% block content %}
<div class="container"> <div class="container">
{% for message in get_flashed_messages() %}
<div class="alert alert-warning">
<button type="button" class="close" data-dismiss="alert">&times;</button>
{{ message }}
</div>
{% endfor %}
{% block page_content %}{% endblock %} {% block page_content %}{% endblock %}
</div> </div>
{% endblock %} {% endblock %}


Loading…
Cancel
Save