0

I'm in the early stages of pset7.

On register, I'm looking to implement flashing messages instead of having the apology redirects, since they look and act more userfriendly.

I understand that flash has different colours built in to its functionality, and want it so if the user enters invalid details on the registration page, they are presented with one colour of flashed message, but if they successfully register, a different coloured message is presented.

My code is like this;

        {% if get_flashed_messages(with_categories=True) %}
            <header>
                {% if category == 'message' %}
                    <div class="alert alert-info fade in" role="alert">
                        <a href="#" class="close" data-dismiss="alert" aria-label="close">&times;</a>
                        {{ get_flashed_messages() | join(" ") }}
                    </div>
                {% else %}
                    <div class="alert alert-warning fade in" role="alert">
                        <a href="#" class="close" data-dismiss="alert" aria-label="close">&times;</a>
                        {{ get_flashed_messages() | join(" ") }}
                    </div>
                {% endif %}
            </header>
        {% endif %}

in layout.html, and, for example

    if not request.form.get("password2"):
        flash('Please confirm your password')
        return render_template("register.html")

    else
        flash('Registration successful', category='message')
        return render_template("login.html")

in application.py.

Right now, the first if statement in layout.html is never fulfilled, meaning all messages have the warning style. I've changed the category of the successful message in application.py, but it still comes back in the warning style instead of the info style.

Any help?

1

There is no "thing" called category. From the Flask flash documentation:

{% with messages = get_flashed_messages(with_categories=true) %}
  {% if messages %}
    <ul class=flashes>
    {% for category, message in messages %}
      <li class="{{ category }}">{{ message }}</li>
    {% endfor %}
    </ul>
  {% endif %}
{% endwith %}

Based on that code, it looks like get_flashed_messages returns a dictionary iterator object (as created with dict.items() perhaps).

dict.items() returns a Dictionary view object. That affords this syntax:
for key, value in dictview. It will populate key and value with the key,value pairs in the dictionary. for category, message in messages from the doc example indicates that the "key" is category and "value" is message.

A quick review of bootstrap.css indicates it has 4 alert classes: alert-info (blue), alert-success (green), alert-warning (pale yellow), alert-danger (pale red). Think about sending the pre-defined alert type instead of creating your own. Think about how you can construct your div within a for loop similar to the <ul> created in the example Think about how to use category to "apply" the desired tone. Maybe something like <div class="alert {{category}}" .....>

4
  • thanks man. i wish i had more of an idea what you were talking about. – Sameer Malik Jun 12 '17 at 18:03
  • Added more of an idea to answer. Now you need to spend time searching, reading, learning, and experimenting. – DinoCoderSaurus Jun 12 '17 at 19:14
  • 1
    You're a legend. I've come across quite a few of your posts whilst browsing stack overflow for help with my learning. Thanks again. – Sameer Malik Jun 12 '17 at 21:03
  • got it working! you absolute legend :D – Sameer Malik Jun 12 '17 at 21:22

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .