Problem description

As part of a Django project, there will inevitably be situations where the back end passes HTML code to the front end. During this process, By default, Django encodes these special HTML symbols as HTML text to display. For example, the back end passes < SPAN >, which is a SPAN tag, to the front end. But the data Django passes is < span> This is a SPAN tag. /span> , successfully transcodes < and > so that the front end displays characters instead of passing them as labels.

Problem solving

In the above problems, we adopt two ways to solve them:

  • usemark_safeMethod to pass unescaped data
    from django.utils.safestring import mark_safe
    
    mark_safe("This is the preceding code.")
    Copy the code
  • usesafeFilter for escaping character rotation
    <div>
        {{ value|safe }}
    </div>
    Copy the code

Either way will do the trick.