How do I display a HTML content stored in a variable in my template with safe and close non closing tags

I have something like this in my Django template :

<div>{{ terms_conditions|safe|default:'N/A' }}</div>

But terms_conditions is a malformed HTML with some tags not closed as a result of which the HTML doesn’t display correctly.

How do I rectify this ?
I don’t want to use escape or striptags because I want the HTML to be rendered.

There’s nothing in the template system itself that can help with this. Depending upon just how badly malformed that data is, you might be able to write an “HTML cleanup” function that would at least prevent some of the potential problems from showing up. (You might find the BeautifulSoup library particularly useful for that.)

But nothing is going to be perfect, and your best defense is to validate this HTML at the point of submission. (Hopefully this content is all coming from a trusted source, otherwise, you’re opening up a massive security hole.)

Content is coming from a trusted source but I can’t revalidate existing data of so many rows in the table.
Is it possible to use iframe to get this shown with malformed HTML ?

I don’t know. You can try it to see how effective it is in your case.

I ended up doing this :

<iframe width="100%" height="500px" frameborder="0" srcdoc='{{ terms_conditions|safe|default:"N/A" }}'></iframe>

But this assumes the content doesn’t have a single quote.