调用python脚本的django显示html代码而不是网页

时间:2016-09-19 20:35:17

标签: python django

我的django应用程序调用python脚本(query.cgi)。但是当我运行它时,网站会显示该脚本的html打印输出,而不是将输出显示为网页。

def query(request):
    if request.method == 'GET':
       output = subprocess.check_output(['python', 'query.cgi']).decode('utf-8')
       return HttpResponse(output, content_type="text/plain")

网页显示:

Content-Type: text/html


<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<link type="text/css" rel="stylesheet" href="css/css_4.css"      media="screen" />
<title>....</title>
</head><body>.....</body></html>

感谢您的帮助!!

1 个答案:

答案 0 :(得分:1)

return HttpResponse(output, content_type="text/plain")

它返回转义HTML的原因是因为您有content_type='text/plain',表示您只是发送纯文本。

尝试将其更改为'text/html'