3

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?

asked Jul 20, 2020 at 17:32
1
  • You just pass as a variable, say "content" and pipe it through "safe": {content|safe}, since you said you have a list of strings, you could use "safeseq". Commented Jul 20, 2020 at 17:41

1 Answer 1

2

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!

answered Jul 20, 2020 at 17:42
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.