Python Django使用HttpResponse返回图片并显示

views

# 1.03 读取图片demo
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))

url('
url(r'^image/(?P.+)/$',views.my_image,name="image"),

', views.read_img, name='read_img'), # 1.03 读取img demo
url(r'^read_img/(?P.+)/$',views.my_image,name="image"),

template

{{param.id}}


你可能感兴趣的:(Python Django使用HttpResponse返回图片并显示)