Home > other >  Sending and rendering HTML syntax via jinja variable
Sending and rendering HTML syntax via jinja variable

Time:10-18

I need some complicated operations to render some dynamic trees in my front-end. But as I can't find any way to run recursion in my jinja or front-end, I take an approach to make a string in my views.py along with HTML syntax and render them in the front-end to get the desired output something like this (As an example, here I skip the original complicated string because there is no need for it):

in 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)

In index.html:

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

My desired output with this code is:

Hi This is from django

  1. abc
  2. mno
  3. xyz

But the obtain output is:

<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.

CodePudding user response:

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

 {{ test | safe }}
  • Related