简体   繁体   中英

Django: include html saved as string in template rendering

I build an HTML table in my views.py function and I want to include it in a template that I already have when rendering. I see that the div is created, but the HTML table is not created. What is the problem?

this is the line in views.py :

render(request, 'aztracker/import_data.html', {'my_html':html_data}) where html_data is like

"<table><tr><th>column1</th></tr><tr><td>data1</td></tr> ....</table>"

and I have this section in my import_data.html :

    <div class="bootstrap-iso">
        <div class="tbl_container_numbers">
            {{ my_html }}
        </div>
    </div>

this is the sanme div after rendering:

    <div class="bootstrap-iso">
        <div class="tbl_container_numbers">

        </div>
    </div>

I found the problem. To render an html code stored as a string we should use the built-in autoescape tag:

    <div class="bootstrap-iso>
        <div class="tbl_container_numbers">
            {% autoescape off %}
                {{ my_html }}
            {% endautoescape %}
        </div>
    </div>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM