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

**URL:** <https://forum.djangoproject.com/t/how-do-i-display-a-html-content-stored-in-a-variable-in-my-template-with-safe-and-close-non-closing-tags/31122>\
**Category:** Templates & Frontend\
**Created:** [May 13, 2024, 12:04pm UTC](https://forum.djangoproject.com/t/how-do-i-display-a-html-content-stored-in-a-variable-in-my-template-with-safe-and-close-non-closing-tags/31122 "2024-05-13T12:04:20Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![anjanesh](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/anjanesh/32/4583_2.png) [@anjanesh](https://forum.djangoproject.com/u/anjanesh)\
**Post date:** [May 13, 2024, 12:04pm UTC](https://forum.djangoproject.com/t/how-do-i-display-a-html-content-stored-in-a-variable-in-my-template-with-safe-and-close-non-closing-tags/31122/1 "2024-05-13T12:04:20Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [May 13, 2024, 12:18pm UTC](https://forum.djangoproject.com/t/how-do-i-display-a-html-content-stored-in-a-variable-in-my-template-with-safe-and-close-non-closing-tags/31122/2 "2024-05-13T12:18:45Z")

</div>

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.)

---

<div class="post-metadata">

**Author:** ![anjanesh](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/anjanesh/32/4583_2.png) [@anjanesh](https://forum.djangoproject.com/u/anjanesh)\
**Post date:** [May 14, 2024, 12:47am UTC](https://forum.djangoproject.com/t/how-do-i-display-a-html-content-stored-in-a-variable-in-my-template-with-safe-and-close-non-closing-tags/31122/3 "2024-05-14T00:47:18Z")

</div>

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 ?

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [May 14, 2024, 1:31am UTC](https://forum.djangoproject.com/t/how-do-i-display-a-html-content-stored-in-a-variable-in-my-template-with-safe-and-close-non-closing-tags/31122/4 "2024-05-14T01:31:39Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![anjanesh](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/anjanesh/32/4583_2.png) [@anjanesh](https://forum.djangoproject.com/u/anjanesh)\
**Post date:** [May 16, 2024, 6:34am UTC](https://forum.djangoproject.com/t/how-do-i-display-a-html-content-stored-in-a-variable-in-my-template-with-safe-and-close-non-closing-tags/31122/5 "2024-05-16T06:34:48Z")

</div>

I ended up doing this :

```auto
<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.
