0

I'm learning Django. I have a views.py file that converts markdown into html. Then this is passed as a variable, called body into the following html. When I view the page, rather than converting the variable body into html, it just displays:

<h1>CSS</h1> <p>Cats can run fast or slow <a href="/animals/HTML">HTML</a> page.</p>

Below is my html file. I'm wondering, in order for the html to be read by the web-browser not as a string but as html, do I do something different then {{body}}?

{% extends "mapper/layout.html" %}

{% block title %}
    mapper
{% endblock %}

{% block body %}
    <h1>{{ title }}</h1>
    <div>
     {{ body }} 
    </div>

{% endblock %}

1 Answer 1

2

To django not escape the incoming variable and you trust that variable, like you are sending an actual html file, then you can choose any of the method.

Either you can use in built block tag like:

{% autoescape off %}
    {{ body }}
{% endautoescape %}

Or you can use filter to that variable:

{{ body|safe|escape }}

Refs: autoescape | safe

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a ton @Biplove Lamichane that's exactly what did the trick!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.