Vba_Beg
Vba_Beg

Reputation: 523

How to put html code into template in Django

Trying to send to template html code from another html file. The view code looks like:

def homepage( request ):

    return render(request, 'homepage.html', {
        'table': generateTable(),
    })

def generateTable():
    x = render_to_response( 'tableSchema.html' )
    return x

And in a template:

{{ table|safe }}

Everything is okay, but I see an information about UTF8 above the table : I mean the table is generating correctly ,but above is the following text:

Content-Type: text/html; charset=utf-8

Do you know why and how to remove it ? Thanks in advance,

Upvotes: 0

Views: 528

Answers (1)

Daniel Roseman
Daniel Roseman

Reputation: 599450

render_to_response does exactly what the name suggests: renders the template and creates a response. You don't want a response, you just want the rendered string: so use render_to_string.

However, a more natural way of doing this would be to use a template tag or include inside the template itself.

Upvotes: 2

Related Questions