Katie Melosto
Katie Melosto

Reputation: 1177

how to convert a variable passed into HTML from python into HTML?

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 %}

Upvotes: 0

Views: 125

Answers (1)

Biplove Lamichhane
Biplove Lamichhane

Reputation: 4095

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

Upvotes: 2

Related Questions