【问题标题】:How to return an image object in django http response?如何在 django http 响应中返回图像对象?
【发布时间】:2020-01-21 16:21:26
【问题描述】:

我正在发送包含图片的电子邮件。我正在为这些电子邮件使用 html 模板。我希望动态生成图像。因此,图像标签中的“src”是一个对我的应用程序进行 REST api 调用的 url。图像是动态创建的,创建了一个可公开访问的 URL。我希望此图像显示在电子邮件中。但我不知道如何返回这张图片。

【问题讨论】:

  • 只需从您的视图返回一个HttpResponse,其中内容类型标头设置为image/jpeg(或任何您的图像格式),content 是您生成的图像。

标签: html django python-3.x image html-email


【解决方案1】:

你可能想要一些这样的代码

    the_image= Image.new(#whatever you want in your image)
    response = HttpResponse(content_type="image/jpeg")
    the_image.save(response, "JPEG")
    return response

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-06-22
    • 2011-03-05
    • 2017-04-05
    • 1970-01-01
    • 1970-01-01
    • 2019-06-25
    • 2018-08-06
    相关资源
    最近更新 更多