Displaying multiple flash messages with flask

6k Views Asked by At

I am trying to display multiple flash messages on my html using flask for whatever errors might be occurring with a validation form. Right now only one does. Is there a way to do this? Possibly with some kind of blank list that the error messages go into and then iterating through that list on the html side?

python:

@app.route("/user", methods=['POST'])

def create_user():

    if len(request.form["name"]) < 1:
        flash("Name cannot be blank")
        return redirect("/")
    else:
        session["name"] = request.form["name"]
        session["location"] = request.form["location"]
        session["language"] = request.form["language"]
    if len(request.form["comment"]) > 120:
        flash("Comment cannot be longer than 120 characters")
        return redirect("/")
    elif len(request.form["comment"]) < 1:
        flash("Comment cannot be blank")
    else:
        session["comment"] = request.form["comment"]

    return redirect("/results")

html:

{% with messages = get_flashed_messages() %}
    {% if messages %}
        {% for message in messages %}
            <p>{{ message }}</p>
        {% endfor %}
    {% endif %}
{% endwith %}
<form action="/user" method="POST" accept-charset="utf-8">
    <label>Name:</label>
    <div id=right>
        <input type="text" name="name">
    </div>
    <label>Location:</label>
    <div id=right>
        <select name="location">
        <option value="location1">Location 1</option>
        <option value="location2">Location 2</option>
        </select>
    </div>
    <label>Language:</label>
    <div id=right>
        <select name="language" >
        <option value="choice1">Choice 1</option>
        <option value="choice2">Choice 2</option>
        </select>
    </div>
    <label>Comment (optional):</label>
    <textarea name="comment" rows="5", cols="35"></textarea>
    <button type="submit">Submit</button>
</form>
1

There are 1 best solutions below

1
On BEST ANSWER

You can definitely show multiple flash messages, this is the default behaviour. The problem is that your code path never allows for multiple flash messages, as you're returning a redirect straight away after the call to flash. You could refactor your code like this:

@app.route("/user", methods=['POST'])

def create_user():

    errors = False

    if len(request.form["name"]) < 1:
        flash("Name cannot be blank")
        errors = True
    else:
        session["name"] = request.form["name"]
        session["location"] = request.form["location"]
        session["language"] = request.form["language"]

    if len(request.form["comment"]) > 120:
        flash("Comment cannot be longer than 120 characters")
        errors = True
    elif len(request.form["comment"]) < 1:
        flash("Comment cannot be blank")
        errors = True
    else:
        session["comment"] = request.form["comment"]

    if errors:
        return redirect("/")
    else:
        return redirect("/results")