1

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!

1 Answer 1

1

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

Comments

Your Answer

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.