0

I am using json2html to convert json values to html tables in my views.py

render(request, 'home.html', {'value': json2html.convert(json=result)})

where result has json data.

The above gives me html for json but in home.html template it is not displayed in html table format instead it is displaying like below on the site

<ul><li><table border="1"><tr><th>name</th><td>Test</td></tr><tr><th>

my home.html the code looks like this

<html>
<body>
{{ value }}
</body>
</html>

the variable value has the converted html table code but it is displaying as raw instead it should render as html code. How to do this ?

Vimalan E
  • 351
  • 3
  • 12

3 Answers3

2

Tell Django that a string is safe and will not be automatically escaped:

{{ value|safe }}
Razenstein
  • 3,532
  • 2
  • 5
  • 17
1

HTML

<script>
$(document).ready(function(){
    $.ajax({
        type: "GET",
        url: "your home view url",
        success: function (response) {  
            $('body').html(response)
        }
    });
})
</script>

views.py

return Response('<ul><li><table border="1"><tr><th>name</th><td>Test</td></tr><tr><th>')

upvote and approve if it helps.

0

I found something called format_html under django.utils which does the job.

so the code looks like

render(request, 'home.html', {'value': format_html(json2html.convert(json=res))})

Thanks

Vimalan E
  • 351
  • 3
  • 12