[Django]-Add rows in a table using reportlab django

2๐Ÿ‘

You are overwriting the data array at every loop step.
You need it to be something like this:

data=[
    ["Inspection_Id", "Licence_Plate", "Images", "Comment"],
    ["100", "TEST", "http://url.to/png", "Qwerty"],
    ["200", "2nd data row", "http://url.to/gif", "Dvorak"]
]

So that you can get something like

| Inspection_Id | Licence_Plate | Images            | Comment |
| ------------- |-------------- | ----------------- | ------- |
| 100           | TEST          | http://url.to/png | Qwerty  |
| 200           | 2nd data row  | http://url.to/gif | Dvorak  |

Therefore, your loop needs to look like:

# Fill the first row of `data` with the heading, only once!
data = [[inspection, licplt, imgs, cmnts]]
try:
    for i in damage_data:
        inspection_data = str(i.inspection_id).encode('utf-8')
        licence_plate = str(i.inspection.vehicle.licence_plate).encode('utf-8')
        images = str(i.image).encode('utf-8')
        comments = str(i.comment).encode('utf-8')
        inspcdata = Paragraph(inspection_data, styleN)
        lncplt = Paragraph(licence_plate, styleN)
        img = Paragraph(images, styleN)
        cmt = Paragraph(comments, styleN)

        # Add this loop's step row into data array
        data += [inspcdata, lncplt, img, cmt]

And that should be it ๐Ÿ™‚

And image data should be as the hyperlink.

What do you mean with this?

Cheers!

๐Ÿ‘คraneq

1๐Ÿ‘

If your main goal is to iterate and show the damage_data records in a table, then try the following:

(...)

damage_data = Damage.objects.all()
data = []
data.append(["Inspection_Id", "License_Plate", "Images", "Comment"])
try:
    for i in damage_data:
        row = []
        inspection_data = str(i.inspection_id).encode('utf-8')
        licence_plate = str(i.inspection.vehicle.licence_plate).encode('utf-8')
        images = str(i.image).encode('utf-8')
        comments = str(i.comment).encode('utf-8')
        row.append(inspection_data)
        row.append(license_plate)
        row.append(images)
        row.append(comments)
        data.append(row)
(...)

This should iterate through your damage_data queryset and populate your table.

๐Ÿ‘คstemado

Leave a comment