Python Django使用HttpResponse返回图片并显示
views.py
def read_img(request): """ : 读取图片 :param request: :return: """ try: data = request.GET file_name = data.get("file_name") imagepath = os.path.join(settings.BASE_DIR, "static/resume/images/{}".format(file_name)) # 图片路径 with open(imagepath, 'rb') as f: image_data = f.read() return HttpResponse(image_data, content_type="image/png") except Exception as e: print(e) return HttpResponse(str(e))
urls.py
url(r'^read_img/(?P<news_id>.+)/$',views.read_img,name="image"),
模板调用
<img src="{% url 'image' param.id %}" alt="{{param.id}}"/>
作者:柒月
Q群 :2122210(嵌入式/机器学习)