0

So, I want to convert a string I have containing HTML tags converted from a Markdown file into actual HTML. Then inserting the HTML afterwards into a template via Django (this is my first time ever using Django).

What my current output looks like: enter image description here

Instead of getting plain text, I want the HTML shown in the screenshot to be executed like normal HTML.

Code from my views.py file:

from django.http.response import HttpResponse
from django.shortcuts import render
from markdown2 import Markdown, markdown
import random
from . import util
import html.parser

# index output
def index(request):
    return render(request, "encyclopedia/index.html", {
        "entries": util.list_entries()
    })

# function to render wiki entries
def page_render(request, title):
    entry = util.get_entry(title)
    if entry != None:
        markdowner = Markdown()
        content = markdowner.convert(entry)
        return render(request, "encyclopedia/display.html", {
        "title": title,
        "content": content
    })
    else:
        return render(request, "encyclopedia/error.html")

Code from my HTML template:

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

{% block title %} 

{{ title }} 

{% endblock %}

{% block body %}
    {{ content }}
{% endblock %}

Thanks in advance for the help!

Kind Regards
PrimeBeat

PrimeBeat
  • 444
  • 5
  • 15
  • Does this answer your question? [Rendering a template variable as HTML](https://stackoverflow.com/questions/4848611/rendering-a-template-variable-as-html) – Abdul Aziz Barkat Jul 26 '21 at 12:00

2 Answers2

3

You can use the safe tag.

Insert content like this : {{ content|safe }}

Omar Siddiqui
  • 1,625
  • 5
  • 18
2

If you don't want the HTML to be escaped, look at the safe filter and the autoescape tag:

safe:

{{ myhtml |safe }}

autoescape:

{% autoescape off %}
    {{ myhtml }}
{% endautoescape %}
code.rider
  • 1,891
  • 18
  • 21