@Annabel
在urls.py 添加
urlpatterns = [
...
url(r'^uploads/(?P<path>.*?)/(?P<subpath>.*?)/(?P<filename>.*?)/$', views.get_upload_file, name="get_upload_file"),
...
]
在view.py添加
import os
from django.http import HttpResponse
def get_upload_file(request, path, subpath, filename):
file_path = os.getcwd() + '/uploads/{}/{}/{}'.format(path, subpath, filename)
print(file_path)
if os.path.isfile(file_path):
try:
with open(file_path, 'rb') as f:
image_data = f.read()
return HttpResponse(image_data, content_type="image/png")
except Exception as e:
print(e)
return HttpResponse(str('404: Not Found'))
这样就可以显示图片啦