Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python Build a Social Network with Flask How to Win Friends Follow and Unfollow Buttons

Anders Axelsen
Anders Axelsen
3,471 Points

In line 132, in the video, what is the ), "succes") for?

I have been dwelling on this several times. Is "succes" a command, function or a string?

Kindly, Anders.

:ocean:

1 Answer

Ari Misha
Ari Misha
19,323 Points

Hiya Anders! The "flash()" works on your sessions and ya need secret_key for that. The "flash()" method takes a "string" arguement, to "flash" on the screen on the next request and with categories as well in some cases. These categories could be anything like "success" associated with your happy process or "fail" category. Like you can categorize your flash messages. And you can render your "flash()" in your templates as well , and yes with categories too. I'll show ya an example to clear away the confusion:

from flask import Flask, flash, redirect, render_template, \
     request, url_for

app = Flask(__name__)
app.secret_key = 'some_secret'

@app.route('/')
def index():
    return render_template('index.html')

@app.route('/login', methods=['GET', 'POST'])
def login():
    error = None
    if request.method == 'POST':
        if request.form['username'] != 'admin' or \
                request.form['password'] != 'secret':
            error = 'Invalid credentials'
        else:
            flash('You were successfully logged in', 'success')
            return redirect(url_for('index'))
    return render_template('login.html', error=error)

Here is how you render messages:

{% 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 %}

Here is how you filter your flash messages on categories in templates:

{% with errors = get_flashed_messages(category_filter=["error"]) %}
{% if errors %}
<div class="alert-message block-message error">
  <a class="close" href="#">Ɨ</a>
  <ul>
    {%- for msg in errors %}
    <li>{{ msg }}</li>
    {% endfor -%}
  </ul>
</div>
{% endif %}
{% endwith %}
Anders Axelsen
Anders Axelsen
3,471 Points

Hi there!

Thank you thank you thank you.

It didn't make 100% sense to me. - But it made SOME sense. I will learn it through repetition!

Kind regards