首页 > 解决方案 > 通过 jinja 变量发送和渲染 HTML 语法

问题描述

我需要一些复杂的操作来我的front-end. 但是由于我找不到在我的jinjaor中运行递归的任何方法front-end,我采取一种方法在我的中创建一个字符串views.py并在其中HTML syntax呈现它们front-end以获得所需的输出,例如(例如,这里我跳过原来复杂的字符串,因为不需要它):

views.py

test = "<h2>Hi This is from django</h2><ol><li>abc</li><li>mno</li><li>xyz</li></ol>"

mydict={
    'test' : test,
}
return render(request, 'app\index.html', mydict)

index.html

<div class="container-fluid">
    {{ test }}
</div>

希望使用此代码的输出是:

嗨,这是来自 django

  1. 美国广播公司
  2. mno
  3. xyz

但是得到的输出是:

<h2>Hi This is from django</h2><ol><li>abc</li><li>mno</li><li>xyz</li></ol>

Please suggest to me, is there any way to render the jinja string variable along with the effect of HTML in my front-end? If not then how can I take an approach to render any tree dynamically in my front-end where the level, leaf node, intermediate node, etc all info come from the database.

标签: pythonhtmldjangojinja2

解决方案


You can use the django-template-filter safe for this.

 {{ test | safe }}

推荐阅读