convert render_pdf to get a list

0

I need to modify the render_pdf so that it receives the data that is stored in a list, since currently it does not show me any information. my code is this:

Views.py

class PDFprueba(View):

def get(self,request,*args,**kwargs):
    datos = []

    for mascota in Mascota.objects.all() :
        datos.append(
            {
            'nombre' : mascota.nombre,
            'sexo' : mascota.sexo,
            }
            )
    return render_pdf('pdf/pdf.html',{'datos': datos})

render_pdf

def render_pdf(template_src, context_dict):
template = get_template(template_src)
html  = template.render(context_dict)
result = BytesIO()

pdf = pisa.pisaDocument(BytesIO(html.encode('UTF-8')), result)
if not pdf.err:
    return HttpResponse(result.getvalue(), content_type='application/pdf')
return HttpResponse('We had some errors<pre>%s</pre>' % escape(html))
    
asked by jose ricardo 18.12.2018 в 23:32
source

1 answer

0

My solution:

The project did not send me the information that was in the model to a pdf, I miss calling the model to the class.

I also needed to obtain the pk to print the information of the pet and not of all the pets that were in the system.

the code is as follows:

class PDFprueba(View):
model = Mascota

def get(self,request,*args,**kwargs):
    pk = self.kwargs.get('pk',0)
    QuerySet = self.model.objects.filter(id=pk)
    datos = []
    for mascota in QuerySet :
        datos.append({
            "id": mascota.id,
            "nombre" : mascota.nombre,
            "sexo" : mascota.sexo
            })

    return render_pdf('pdf/pdf.html',{'datos': datos})

do not modify the file "render_pdf"

    
answered by 21.12.2018 / 23:45
source