Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTML not rendering well when using markdown2 converted

I'm working on a Django project in Python and using markdown2 to convert some markdown text into HTML but it appears to not be doing it well or I'm missing something in the code.

In my views.py file I'm using a custom function to extract some text from an .md file in markdown format and with the markdown2 module I try pass it on to the HTML template like so:

text = markdown2.markdown(util.get_entry(entry))

But in the HTML, if I inspect the source code what is supposed to render as

HTML

comes out like this:
<h1>HTML</h1>

Am I missing something in my code? Is the error on the HTML template or in my views.py file?

Thanks in advance!

like image 695
foreverajohn Avatar asked Jul 09 '20 20:07

foreverajohn


1 Answers

You probably have rendered the content in the template with:

{{ text }}

Django will by default escape the content, and thus replace < with &lt;, etc.

You can make use of the |safe template filter [Django-doc] to prevent this:

{{ text|safe }}
like image 91
Willem Van Onsem Avatar answered Sep 28 '22 01:09

Willem Van Onsem