[Django]-How to add images to python docxtpl from url

3๐Ÿ‘

โœ…

I found out what to do:

In my model I added the following (I use a model with image urls in a char field called ImageURL)

from django.db import models
import requests
import io

class myModel(models.Model)
    ImageURL = models.CharField(max_length=250, null=True, blank=True)
    

    @property
    def product_image(self):
        "Returns the product image"
        response = requests.get(self.ImageURL)
        image_bytes = io.BytesIO(response.content)
        return image_bytes

You can use this property in the inlineimage class of docxtpl and it works

๐Ÿ‘คSander

Leave a comment