Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to flashing a message with link using Flask flash?

I'm creating a web app using Flask to deal with GoogleOpenID, these codes are almost finished, except the flashing message contains a link:

@oid.after_login
def create_or_login(resp):
    user = db_session.query(User).filter_by(email=resp.email).first()
    if user is not None:
        flash('Successfully signed in', 'success')
    else:
        user = User(nickname=resp.fullname, source=GOOGLE, email=resp.email)
        db_session.add(user)
        db_session.commit()
        flash(flashing_message, 'success')
    g.user = user
    session['nickname'] = user.nickname
    return redirect(oid.get_next_url())

It works well when flashing_message is like this: 'Successfully registered, please click here'

But when flashing_message is 'Successfully registered, please click <a href="/me" class="alert-link">here</a>', it doesn't work (flashes nothing) without throwing any Error. Strangely, sentences between flash() and return doesn't work either (did not set session['nickname] or g.user).

like image 843
Kane Blueriver Avatar asked Jan 21 '14 03:01

Kane Blueriver


People also ask

Why is Flash not working flask?

Calling flash() is not enough to get messages displayed; the templates used by the application need to render these messages. So anyone who have the same problem, just ensure, that you added the get_flashed_messages() function to the template and then displayed a message.

What is flash message HTML?

Flash messages are used to provide a quick feedback (e.g., to confirm a setting has been updated) to the user. To connect a flash message to a trigger (e.g., a button), make sure the id value of the first one is equal to the value of the aria-controls of the second one.


2 Answers

The other answers here focus on changing your template to allow all flash messages to be marked as safe, which may not be what you want.

If you just want to mark certain flashed messages as safe, wrap the text passed to flash() in Markup(). (Flask API Docs for Markup)

For example, instead of:

flash('Successfully registered, please click <a href="/me" class="alert-link">here</a>')

Wrap the string in Markup() like this:

flash(Markup('Successfully registered, please click <a href="/me" class="alert-link">here</a>'))

As always, you will need to import Markup from the flask package something like:

from flask import Markup
like image 123
Pete Watts Avatar answered Sep 19 '22 15:09

Pete Watts


You need to render a template after calling flash() which should then get the message using get_flashed_messages(). You need to adjust your code to call a template after calling flash(). flash sends the message to next request which can be extracted by the template. The template can look something like:

{% with messages = get_flashed_messages() %}
    {% if messages %}
    <ul class=flashes>
    {% for message in messages %}
        <li>{{ message | safe }}</li>
    {% endfor %}
    </ul>
  {% endif %}
{% endwith %}

In your view code, I would add a render_template right after the flash() call. Something like:

flash('success')
return render_template('whatever.html')   
like image 22
codegeek Avatar answered Sep 18 '22 15:09

codegeek