Django: Multiple Content-Type: text/html appears on html page

1.5k views Asked by At

I'm working on a Django project that accesses different tables from my database and outputting that information all on one page. The way I'm doing that is:

a = get_hostname(request, hostname)
b = get_systeam(request, hostname)
c = get_appteam(request, hostname)

response = HttpResponse()
response.write(a)
response.write(b)
response.write(c)
return HttpResponse(response)

Where all the called functions return HTTPresponse objects through render_to_response methods. When I use Javascript to see the concatenated html, there are multiple Content-Type:text/html; charset=utf-8 that appear on the page.

<h2>
    <div class ="row">
    <center>
      <div class ="col-md-3 col-md-offset-3">
        <h1>Hostname: xxxxxxx </h1>
      </div>
</h2>
Content-Type: text/html; charset=utf-8



<h3>

    <div class ="row">

      <div class ="col-md-3 col-md-offset-2">
        <h1>System Team: xxxxxx </h1>
      </div>

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


      <div class = "col-md-3 col-md-offset-2">

        <h1>Application Team: xxxxxxxx </h1>
      </div>


    </div>
</h3>

I have a feeling the meta tag in what is my base.html isn't being passed so when the html chunks are being rendered they automatically add the Content-Type lines. Any help is appreciated!

1

There are 1 answers

1
Daniel Roseman On BEST ANSWER

This has nothing to do with meta tags.

You should only create one HttpResponse. Your functions should not use render_to_response, they should either use render_to_string or just directly render their contents and return them.

Also, in your function response is already an HttpResponse, so you should just return it at the end rather than wrapping it in yet another HttpResponse.