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
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')
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
I am not sure if it is correct, but the way I solved this was by declaring another variable in the function that was a string of HTML and then passing it through a render_template()
function.
And in the template passed it through the safe
filter.
For example, (roughly based on) the code you have provided:
@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')
link = "<a href=\"some_link.html\">Link</a>" ## ADDED HTML WITH LINK ##
g.user = user
session['nickname'] = user.nickname
return render_template('some_layout.html',
link=link ## PASS THE LINK TO THE TEMPLATE ##
)
Then in the template I added an extra if statement inside the get_flashed_messages()
if:
{% with messages = get_flashed_messages(with_categories=true) %}
{% if messages %}
... Code that was already here ...
{% if link %} ## PASSED THE LINK TO THE TEMPLATE HERE ##
{{ link | safe }}
{% endif %}
{% endif %}
{% endwith %}
Escaping HTML is the default behavior, so pass it through the safe
filter to display the HTML unescaped:
{{ message|safe }}