how to render django template variable as html?

前端 未结 4 694
温柔的废话
温柔的废话 2021-02-13 14:16

What I want is like stack overflow. User can HTML format their text input, and the page should be rendered exactly in the same way,

I use the wmd.js to stor

相关标签:
4条回答
  • 2021-02-13 14:50

    I think another approach for this is to use the firstof tag:

    Note that the variables included in the firstof tag will not be escaped. This is because template tags do not escape their content. Any HTML or Javascript code contained in the printed variable will be rendered as-is, which could potentially lead to security issues. If you need to escape the variables in the firstof tag, you must do so explicitly

    {% firstof variable %}
    
    0 讨论(0)
  • 2021-02-13 15:00

    use tag : http://docs.djangoproject.com/en/dev/ref/templates/builtins/#autoescape

    {% autoescape off %}{{ variable }}{% endautoescape %} 
    
    0 讨论(0)
  • 2021-02-13 15:05

    For simple HTML formatting, use <p>{{something}}</p>. And the Javascript way is,

    <script type="text/javascript">
        var variable  =  "<p>{{something}}</p>";
        document.write(variable);
    </script>
    

    If that {{something}} itself contains the HTML tags, then {{something|safe}} itself should work unless you have {% autoescape on %}. For more filtering and formatting refer Built-in template tags and filters.

    0 讨论(0)
  • 2021-02-13 15:11

    You may want to use escape to render it, instead of safe.

    {{ variable|escape }}
    
    0 讨论(0)
提交回复
热议问题