Home > Mobile >  How to make django not render html?
How to make django not render html?

Time:12-31

I have model:

class NewsItem(models.Model):
    meta_title = models.CharField(_('Meta title'), max_length=255, blank=True, null=True,
                              help_text=mark_safe('Tag <title>'))

But on the page I have:

<div >
    <div>
        <label for="id_meta_title">Meta title:</label>
        <input type="text" name="meta_title"  maxlength="255" id="id_meta_title">
        <div >Tag <title></div>
    </div>
</div>

How to tell django not render html?

CodePudding user response:

mark_safe(…) [Django-doc] tells Django not to escape the HTML. It will thus not translate < to &lt;, etc. and as a result, the HTML ends up as HTML code in the rendered output.

By omitting the mark_safe(…) part, you thus will render this as Tag &lt;Title&gt;, which will appear on the page as Tag <title>.

Django thus automatically escapes HTML content written as help text, values, etc. You should only use mark_safe(…) if you want to render the HTML as HTML content.

  • Related