paulohcarellos
paulohcarellos

Reputation: 47

How to dinamically inject HTML code in Django

In a project of mine I need to create an online encyclopedia. In order to do so, I need to create a page for each entry file, which are all written in Markdown, so I have to covert it to HTML before sending them to the website. I didn't want to use external libraries for this so I wrote my own python code that receives a Markdown file and returns a list with all the lines already formatted in HTML. The problem now is that I don't know how to inject this code to the template I have in Django, when I pass the list to it they are just printed like normal text. I know I could make my function write to an .html file but I don't think it's a great solution thinking about scalability.

Is there a way to dynamically inject HTML in Django? Is there a "better" approach to my problem?

Upvotes: 3

Views: 541

Answers (1)

Gaspard Merten
Gaspard Merten

Reputation: 1233

You could use the safe filter in your template! So it would look like that.

Assuming you have your html in a string variable called my_html then in your template just write {{ my_html | safe }}

And don’t forget to import it!

Upvotes: 2

Related Questions