0

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?

2
  • I FIXED IT. Just put {{ entry|safe }} Commented Oct 19, 2020 at 13:47
  • Do you have an example of the markdown file? Commented Oct 19, 2020 at 13:47

1 Answer 1

3

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 %}
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.