I am trying to render html to pdf and include it as a django action, however, for some odd reason, I seem to keep getting the issue that the object has no attribute 'some_attribute'.
My view looks like this:
class CreatePdf(ContextMixin, View):
def get_context_data(self, obj, request):
cxt = {'some_attribute': obj.some_attribute,
'today': date.today()}
return cxt
def create_pdf(self, some_template, some_dict={}):
template = get_template(some_template)
html = template.render(some_dict)
result = BytesIO()
pdf = pisa.pisaDocument(BytesIO(html.encode("ISO-8859-1")), result)
if not pdf.err:
return HttpResponse(result.getvalue())
return None
And the action, which had the purpose to call everything and do all the magic, looks like this:
def get_pdf(self, obj, request):
pdf_view = views.CreatePdf()
pdf = pdf_view.create_pdf('templates/some_template.html', pdf_view.get_context_data(obj,request))
return HttpResponse(pdf)
I thought, that using get_context_data
will get the fields of the selected object in the admin field, however, it seems to through me the same error.
Last, but not least, in my admin.py
I have:
class MyAdmin(admin.ModelAdmin):
actions = [get_pdf]