0

我正在开发一个 Django 项目,该项目从我的数据库中访问不同的表,并将这些信息全部输出在一个页面上。我这样做的方式是:

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)

所有被调用的函数都通过 render_to_response 方法返回 HTTPresponse 对象。当我使用Javascript查看连接的html时,有多个Content-Type:text/html; charset=utf-8 出现在页面上。

<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>

我有一种感觉,我的 base.html 中的元标记没有被传递,所以当 html 块被呈现时,它们会自动添加 Content-Type 行。任何帮助表示赞赏!

4

1 回答 1

0

这与元标记无关。

您应该只创建一个 HttpResponse。你的函数不应该使用 render_to_response,它们应该使用 render_to_string 或者直接渲染它们的内容并返回它们。

此外,在您的函数response中已经是一个 HttpResponse,因此您应该只在最后返回它,而不是将它包装在另一个 HttpResponse 中。

于 2014-07-28T23:07:08.503 回答