Django 流式http响应未显示完整内容
我正在使用Django 流式http响应未显示完整内容,django,Django,我正在使用django创建一个网站。我使用StreamingHttpResponse加载网页,详细信息如下 我的视图代码: from django.http.response import StreamingHttpResponse class SearchNews: def get(self, request): response = StreamingHttpResponse(self._load_stream(request)) retur
django
创建一个网站。我使用StreamingHttpResponse
加载网页,详细信息如下
我的视图
代码:
from django.http.response import StreamingHttpResponse
class SearchNews:
def get(self, request):
response = StreamingHttpResponse(self._load_stream(request))
return response
def _load_stream(self, request):
query = request.GET.get('query', '')
yield loader.get_template('news_results_part1.html').render({
'request': request,
'query': query,
})
api = SearchNews()
results = api.search(query)
yield loader.get_template('news_results_part2.html').render({
'request': request,
'query': query,
'results': results,
})
我有两个html部分,第一部分只包含标题,第二部分包含搜索结果
第一个html部分代码:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width">
<title>
{{ query }}
</title>
<style>
{% include 'internal_static/css/search_results.css' %}
</style>
</head>
<body>
<div class="border-bottom clearfix">
<div class="max-width-large w-100 padding-left-lg">
{% include 'header.html' with query=query %}
</div>
</div>
<div>
{% if extra_info.correct_query %}
<div class="correct-query-wrapper padding-left-lg max-width-large pt-3 clear-both w-100 text-right">
<span class="float-right text-danger">did you mean?</span>
<span class="p-2 font-italic">
<a href="{% url 'search' %}?query={{ correct_query }}">
{{ correct_query }}
</a>
</span>
</div>
{% endif %}
<div class="padding-left-lg clearfix result-cart-wrapper max-width-large clear-both">
{% include 'result_carts.html' with query=query %}
</div>
</div>
</body>
</html>
{{query}
{%include'internal_static/css/search_results.css%}
{%include'header.html'和query=query%}
第二个html部分代码:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width">
<title>
{{ query }}
</title>
<style>
{% include 'internal_static/css/search_results.css' %}
</style>
</head>
<body>
<div class="border-bottom clearfix">
<div class="max-width-large w-100 padding-left-lg">
{% include 'header.html' with query=query %}
</div>
</div>
<div>
{% if extra_info.correct_query %}
<div class="correct-query-wrapper padding-left-lg max-width-large pt-3 clear-both w-100 text-right">
<span class="float-right text-danger">did you mean?</span>
<span class="p-2 font-italic">
<a href="{% url 'search' %}?query={{ correct_query }}">
{{ correct_query }}
</a>
</span>
</div>
{% endif %}
<div class="padding-left-lg clearfix result-cart-wrapper max-width-large clear-both">
{% include 'result_carts.html' with query=query %}
</div>
</div>
</body>
</html>
{%if extra_info.correct_query%}
你是说?
{%endif%}
{%include'result_carts.html'和query=query%}
当我在localhost
中运行项目时,一切正常,浏览器显示第一部分的全部内容,收到搜索结果后显示第二部分。但当我在服务器上部署项目时,浏览器只显示第一部分的一小部分,在收到第二部分后,完成第一部分并同时显示第二部分
如何修复它?问题出在
nginx
配置中。我必须在nginx
中通过添加以下两行来禁用缓冲:
proxy_buffering off;
proxy_request_buffering off;
完整的nginx
配置文件如下:
server {
location / {
proxy_pass http://localhost:8000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
proxy_buffering off;
proxy_request_buffering off;
add_header Last-Modified $date_gmt;
if_modified_since off;
expires off;
etag off;
}
}