Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to display html content through flask messages?

Tags:

python

flask

I understand that flash() takes only string and displays that in the redirected page. I m trying to send html through flash

message = "<h1>Voila! Platform is ready to used</h1>" flash(message) return render_template('output.html') 

output.html

<div class="flashes">   {% for message in get_flashed_messages()%}     {{ message }}   {% endfor %} </div> 

But it is displaying as string <h1>Voila! Platform is ready to used</h1> is there any way to overcome this.

like image 657
naga4ce Avatar asked Aug 14 '13 07:08

naga4ce


People also ask

How do you show messages on a flask?

The flash() method is used to generate informative messages in the flask. It creates a message in one view and renders it to a template view function called next. In other words, the flash() method of the flask module passes the message to the next request which is an HTML template.

Can I use HTML with flask?

Flask uses the Jinja template engine to dynamically build HTML pages using familiar Python concepts such as variables, loops, lists, and so on.


1 Answers

Where possible, a secure approach is to wrap your string in a Markup object before passing it to the template:

Python code:

from flask import Markup  message = Markup("<h1>Voila! Platform is ready to used</h1>") flash(message) return render_template('output.html') 

Jinja2 Template:

<div class="flashes">   {% for message in get_flashed_messages() %}     {{ message }}   {% endfor %} </div> 

Using {{message|safe}} will work, but also opens up the door for an attacker to inject malicious HTML or Javascript into your page, also known an an XSS attack. More info here if you're interested.

like image 179
the911s Avatar answered Sep 22 '22 05:09

the911s