Ad
Why Are Flash-messages Not Displayed?
I am using Flask with Python 3. I made a page which contains a form. After submitting the form, I hope that a message will appear. But the template does not display it.
This is strange because I did everything according to the documentation.
My controller:
from flask import render_template, flash, redirect
from app import app
from app.forms import SearchForm
@app.route('/')
@app.route('/index', methods=['GET', 'POST'])
def index():
form = SearchForm()
if form.validate_on_submit():
flash('search ok')
return redirect('/index')
return render_template('index.html', title='search', form=form)
My form:
from flask_wtf import FlaskForm
from wtforms import StringField, PasswordField, BooleanField, SubmitField
from wtforms.validators import DataRequired
class SearchForm(FlaskForm):
search = StringField('Поиск по фразам', validators=[DataRequired()])
submit = SubmitField('Начать')
My tpl:
{% extends "base.html" %}
{% with messages = get_flashed_messages() %}
{% if messages %}
<ul>
{% for message in messages %}
<li>{{ message }}</li>
{% endfor %}
</ul>
{% endif %}
{% endwith %}
<br>
{% block content %}
<form action="" method="post" novalidate>
{{ form.hidden_tag() }}
<div>
{{ form.search.label }}<br>
{{ form.search(size=32) }}<br>
{% for error in form.search.errors %}
<span style="color: red;">[{{ error }}]</span>
{% endfor %}
</div>
<div>{{ form.submit() }}</div>
</form>
{% endblock %}
My repository is here on GitLab.
Please help me display flash-message.
Ad
Answer
Try moving your flash messages so they appear inside the content
block, like so:
{% extends "base.html" %}
{% block content %}
{% with messages = get_flashed_messages() %}
{% if messages %}
<ul>
{% for message in messages %}
<li>{{ message }}</li>
{% endfor %}
</ul>
{% endif %}
{% endwith %}
<br>
<form action="" method="post" novalidate>
{{ form.hidden_tag() }}
<div>
{{ form.search.label }}<br>
{{ form.search(size=32) }}<br>
{% for error in form.search.errors %}
<span style="color: red;">[{{ error }}]</span>
{% endfor %}
</div>
<div>{{ form.submit() }}</div>
</form>
{% endblock %}
Ad
source: stackoverflow.com
Related Questions
- → What are the pluses/minuses of different ways to configure GPIOs on the Beaglebone Black?
- → Django, code inside <script> tag doesn't work in a template
- → React - Django webpack config with dynamic 'output'
- → GAE Python app - Does URL matter for SEO?
- → Put a Rendered Django Template in Json along with some other items
- → session disappears when request is sent from fetch
- → Python Shopify API output formatted datetime string in django template
- → Can't turn off Javascript using Selenium
- → WebDriver click() vs JavaScript click()
- → Shopify app: adding a new shipping address via webhook
- → Shopify + Python library: how to create new shipping address
- → shopify python api: how do add new assets to published theme?
- → Access 'HTTP_X_SHOPIFY_SHOP_API_CALL_LIMIT' with Python Shopify Module
Ad