amiller_lite
amiller_lite

Reputation: 89

Markdown2 text rendering as a string using Python and Django

I'm working on a project that takes in a markdown page and converts it into HTML before inserting it into the correct document. This is the code I'm running

Python

def markdown(request, entry):
pathOfFile = "entries/" + entry + ".md"
return render(request, "encyclopedia/entry.html", {
    "html_markdown": markdown2.markdown_path(pathOfFile)
})

HTML

{% block body %}
<div>
    {{ html_markdown }}
</div>
{% endblock %}

And this is what is returning on the web page

<h1>CSS</h1> <p>CSS is a language that can be used to add style to an <a href="/wiki/HTML">HTML</a> page.</p>

When I inspect the source of the page the HTML is encased in quotes. Is the problem that my html_markdown variable is being read as a string? What steps can I take to get the HTML to render properly? Thanks in advance for your help!

Upvotes: 1

Views: 582

Answers (1)

willeM_ Van Onsem
willeM_ Van Onsem

Reputation: 477676

html_markdown will contain raw HTML, so if you render that in the template, it will escape characters like < to &lt;, etc.

You can mark the string as "safe" with the |safe template filter [Django-doc] to prevent escaping these characters:

{% block body %}
<div>
    {{ html_markdown|safe }}
</div>
{% endblock %}

Upvotes: 1

Related Questions