이미지 파일을 다운로드 하여 보여주는 코드 예
views.pyfrom django.http import HttpResponse
from django.http import FileResponse
import os
import pandas
import matplotlib.pyplot as plt
import numpy as np
from pandas import DataFrame, Series
import StringIO
def index(request):
return HttpResponse("dir : %s <br><img src='image1'>" % (os.getcwd()))
def image1(request):
response = FileResponse(open('image.png', 'rb'), content_type='image/png')
return response
참고링크
https://docs.djangoproject.com/en/1.9/ref/request-response/#django.http.HttpResponse
HttpResponse 를 이용한 구현 예
def image2(request):
response = HttpResponse(open('image.png', 'rb'), content_type='image/png')
response['Content-Disposition'] = 'attachment; filename="image2.png"'
return response
Pandas Series 차트 이미지를 다운로드 하는 예
import pandas
import matplotlib.pyplot as plt
import numpy as np
from pandas import DataFrame, Series
import StringIO
def chart1(request):
s1 = Series([1,3,2,4,5])
s1.plot(title="g")
bufferIO = StringIO.StringIO()
plt.savefig(bufferIO)
bufferIO.seek(0)
response = HttpResponse(bufferIO, content_type='image/png')
response['Content-Disposition'] = 'attachment; filename="chart.png"'
return response
댓글 없음:
댓글 쓰기