Python 如何使用HttpResponse在django中显示图像
我试图使用下面的行显示python脚本输出的图像,但不是在浏览器中显示,而是下载文件而不是显示代码 这是我在views.py中创建的函数: def adc(请求): file=“C:\Users\TheBoss\Downloads\New\u test.xlsx” df=pd.read\u excel(文件、工作表\u name='Graph')Python 如何使用HttpResponse在django中显示图像,python,django,httpresponse,Python,Django,Httpresponse,我试图使用下面的行显示python脚本输出的图像,但不是在浏览器中显示,而是下载文件而不是显示代码 这是我在views.py中创建的函数: def adc(请求): file=“C:\Users\TheBoss\Downloads\New\u test.xlsx” df=pd.read\u excel(文件、工作表\u name='Graph') 通常,这应该足以以内联方式显示图像 def adc(request): file = "C:\Users\TheBoss\Down
通常,这应该足以以内联方式显示图像
def adc(request):
file = "C:\Users\TheBoss\Downloads\New_test.xlsx"
df = pd.read_excel(file, sheet_name='Graph')
plt.plot(df['Date'], df['Video Device - Not Responding'], label = 'Video Device - Not Responding')
plt.xticks(rotation=45)
plt.tick_params(axis='x', which='major', labelsize=6)
plt.ylabel('Condition Count')
plt.title('Condition')
plt.legend()
buffer = io.BytesIO()
plt.savefig(buffer, format='png')
return HttpResponse(buffer.getvalue(), content_type="test/png")
它应该在大多数浏览器中显示为图像,如果您想在HTML中插入图像,您可以使用一个简单的
<img src="{% url 'my_image' %}">
我从经验中知道,这在Edge、Firefox和Opera中都有效。有时浏览器需要额外的说服力才能以内联方式显示图像,在这种情况下,将标题
Content Disposition
设置为inline
通常有效。尝试设置Content Disposition
标题设置为inline
。这是一个如何做到这一点的例子。我认为Django会自动将其设置为附件
,以便进行文件响应。关于内容配置的附加信息是。感谢您的回复…..我尝试了,但仍然下载了它而不是显示…..谢谢,我尝试了,但下载的文件也没有显示
<img src="{% url 'my_image' %}">