【发布时间】:2020-10-01 13:56:21
【问题描述】:
我正在尝试使用图像 URL 将每个图像实例转换为 base64。所有图像都存储在我的 amazon-s3 存储桶中。不幸的是,我生成的加密没有在我的 recipe_plain.html 模板中显示图像。任何帮助表示赞赏。
views.py
...
import base64
class RecipePlainView(DetailView):
model = Recipe
template_name = 'recipes/recipe_plain.html'
def get_context_data(self, **kwargs):
context = super(RecipePlainView, self).get_context_data(**kwargs)
image = self.object.image
image.open(mode='rb')
context['recipe_image_base64'] = base64.b64encode(image.read())
image.close()
return context
recipe_plain.html
<img src="data:image;base64,{{ recipe_image_base64 }}" alt="{{ recipe.image.name }}">
【问题讨论】:
标签: python html django amazon-s3 base64