当前位置: 动力学知识库 > 问答 > 编程问答 >

Difference between rendering {{ var }} and {{ var|safe }} in Django

问题描述:

Say var is a variable passed with return from a view to a template. Could you please give me a simple example to understand the difference between rendering {{ var }} on the one hand and {{ var|safe }} on the other? I was not able to fully understand what is going on in the documentation: 1, 2. In which case |safe protects me? Thank you very much!

网友答案:

The safe template filter allows html tags and entities in the content of var. Without it, your html will be escaped so you'll see things like

<div> &nbsp;

etc on your page.

网友答案:

If var has html elements then the safe template filter will render it.

For example:

If var is hi

var|safe will be rendered hi var will be displayed <b>hi</b>

网友答案:

Simply saying django's safe method in template doesn't render any html tags in template

分享给朋友:
您可能感兴趣的文章:
随机阅读: