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 ?

3 Answers 3

1

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

{{ value|safe }}
Sign up to request clarification or add additional context in comments.

Comments

0

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.

1 Comment

Thanks. But I wanted to render a template to which I need to pass html code as context and display.
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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.