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}}"/>
原文地址:https://www.cnblogs.com/Ph-one/p/11844611.html