Django:HTML页面上出现多个Content-Type: text/html
我正在做一个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中的meta标签没有被传递,所以在渲染html片段时,它们自动添加了Content-Type这一行。希望能得到一些帮助!
1 个回答
0
这和元标签没关系。
你只需要创建一个 HttpResponse。你的函数不应该使用 render_to_response,而是应该使用 render_to_string,或者直接渲染内容并返回。
另外,在你的函数中,response
已经是一个 HttpResponse 了,所以你最后只需要直接返回它,而不是再包裹一层 HttpResponse。