简体   繁体   中英

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

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!

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.

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