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}}"/>

 

posted @ 2019-11-12 19:38  瘋耔  阅读(1863)  评论(0编辑  收藏  举报
跳至侧栏