I have a simple website where user can enter an email. A flash message is displayed if the email was entered.
If the form is submitted via click with the mouse on the submit button it works fine.
If the form is submitted via keyboard press "enter" the flash message is not displayed.
In both cases the form submits succesfully and adds an entry to the DB.
Its my second project and I never observed such behaviour, probably I never noticed it, will check everywhere now.
Thats how I display flash:
{% with messages = get_flashed_messages() %}
{% if messages %}
<div class="flashes col-xs-12">
{% for message in messages %}
<p>{{ message }}</p>
{% endfor %}
</div>
{% endif %}
{% endwith %}
The Code:
@app.route('/', methods=["GET","POST"])
def index():
form = NewsletterForm()
if form.validate_on_submit():
newsletter_user = NewsletterUser(email=form.email.data.lower().strip())
try:
if newsletter_user:
db_session.add(newsletter_user)
db_session.commit()
flash('Danke, wir melden uns bei Ihnen')
return redirect(url_for("index"))
else:
flash('Email konnte nicht eingetragen werden')
return redirect(url_for("index"))
except IntegrityError:
flash('Email wurde bereits eingetragen')
return redirect(url_for("index"))
return render_template('index.html', form = form)
Here is the part in the HTML (I moved the flash message under the button):
<div class="bg-image" alt="Baubedarf">
<div class="index-container">
<h1> Baubedarf finden. Bautechnik entdecken </h1>
<p class="under-h1 hidden-xs hidden-sm"> Das Branchenverzeichnis für die Bauindustrie und Zulieferer </p>
<div class="newsletter-form col-xs-12 col-sm-10 col-sm-offset-1 col-md-8 col-md-offset-2 col-lg-6 col-lg-offset-3">
<form class="" method="POST" action="{{ url_for('index') }}">
{{ form.csrf_token }}
<h2> Werden Sie bereits von all Ihren potenziellen Kunden gefunden? </h2>
<p class="hidden-xs"> Wenn nicht, dann bieten wir genau das richtige. Sichern Sie sich 6 Monate kostenlos alle Vorteile einer Premium Mitgliedschaft. Melden Sie sich direkt an! </p>
{{ form.email(class="form-control", placeholder="Email") }}
<button type="submit" class="btn"> Eintragen </button>
<img alt="Banner" class="banner-index hidden-xs" src="../static/img/banner-index.png">
{% if form.email.errors %}
{% for error in form.email.errors %}
<p class="flashes-error-form"> {{ error }}</p>
{% endfor %}
{% endif %}
{% with messages = get_flashed_messages() %}
{% if messages %}
<div class="flashes col-xs-12">
{% for message in messages %}
<p>{{ message }}</p>
{% endfor %}
</div>
{% endif %}
{% endwith %}
</form>
</div>
</div>
</div>
Here is the CSS style for the flash message:
.index-container .flashes {
color: white;
text-align: center;
font-size: 20px;
}
EDIT
I created now a new site which should be displayed when the email was added successfully, but it seems the return redirect is not working thats probably why the flash message is not working (Eventhough it also works sometimes).
The code is as simple as it can be... and I still have no clue what causes this issue, because EVERY email is succesfully added to the DB.
How do I create a bounty on this question? Its the weirdest thing I even encountered.
New part, still same problem:
try:
db_session.add(newsletter_user)
db_session.commit()
flash('Danke, wir melden uns bei Ihnen')
return redirect(url_for("newsletter_success"))
except IntegrityError:
flash('Email wurde bereits eingetragen')
return redirect(url_for("index"))
except InvalidRequestError as e:
print e
flash('Email wurde bereits eingetragen')
return redirect(url_for("index"))