Django Pdf HTML Template give values to html

I've been trying to make a Data export for my website, in which the user can download informations about the items.

I do have a function to render the template and stuff, and using a dummy template it all worked. But now i wanted to put actual data from the Database in the template, and i don't know how i should do it.

This is the function to convert the html to pdf:

def html_to_pdf(template_src, context={}):
 template = get_template(template_src)
 html  = template.render(context)
 result = BytesIO()
 pdf = pisa.pisaDocument(BytesIO(html.encode("ISO-8859-1")), result)
 if not pdf.err:
     return HttpResponse(result.getvalue(), content_type='application/pdf')
 return None

And this is the View of the PDF:

class GeneratePdf(View):
 def get(self, request, *args, **kwargs):
    item_list = Item.objects.filter(company = getCompany(request.user))
    pdf = html_to_pdf('pdf/items_pdf.html', item_list)

    return HttpResponse(pdf, content_type='application/pdf')

So as it looks, I cannot give Querysets like item_list to the template. How can I give all of the Items to the template, so that I can loop over them?

Back to Top