Print model data to a pdf

0

I need to show data from the database to a pdf, until now it generates the pdf but with static data that I added to it

VIEWS.PY

class PDFprueba(View):

    def get(self,request,*args,**kwargs):
        datos = {
            'nombre' : 'jose',
            'apellido' : 'florez'
        }
        pdf= render_pdf('pdf/pdf.html',{'datos': datos})

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

MODEL.PY

class Persona(models.Model):
    nombre = models.CharField(max_length=50)
    apellido = models.CharField(max_length=70)
    edad = models.IntegerField()
    telefono = models.CharField(max_length=12)
    email = models.EmailField()
    domicilio = models.TextField()
    
asked by jose ricardo 17.12.2018 в 23:29
source

1 answer

0

First you have to define what your query will be and make it work with the queryset of Django .

If you want to show all the data that you have inserted in your table (model) Person, it would be something like this:

from camino.de.proyecto.models import Persona

...

...
        def get(self,request,*args,**kwargs):
            datos = []
            for persona in Persona.objects.all():
                datos.append({
                    'nombre' : persona.nombre,
                    'apellido' : persona.apellido,
                })
            pdf = render_pdf('pdf/pdf.html',{'datos': datos})

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

You will also have to modify your render_pdf to read datos as a list.

    
answered by 18.12.2018 в 03:02