Reputation: 371
i'm currently working on a proyect that involve "markdown2" librarie to transform .MD files to HTML tags. Almost everything works, but when i render HTML file in python and throw the content of .MD file converted, it just shows the literal tags. Here is the function that take the MD file and read it:
def get_entry(title):
try:
f = default_storage.open(f"entries/{title}.md")
return f.read().decode("utf-8")
except FileNotFoundError:
return None
And here is the function that convert the MD file, pass those HTML tags and render the HTML file:
def entry(request, title):
entry = markdown(util.get_entry(title))
return render(request, "encyclopedia/entry.html", {
"entry" : entry
})
Here is the HTML file:
{% extends 'encyclopedia/layout.html' %}
{% block title %}
{% endblock %}
{% block body %}
{{ entry }}
{% endblock %}
In the browser it shows the HTML file withe converted MD files like this: enter image description here
¿How can i pass those HTML tags so the browser understand that they are tags and not strings?
Upvotes: 0
Views: 412
Reputation: 4095
Django automatically escapes HTML special characters for security reasons, see their docs on autoescape.
When your template variable contains HTML code you need to mark it as safe using the safe
template filter and make sure yourself that any dangerous content (e.g. user input) is filtered:
{% block body %}
{{ entry|safe }}
{% endblock %}
Upvotes: 3