the_game
the_game

Reputation: 261

django output pdf

Hi all as i in learning stage of django so support me. I have to generate pdf reports in django.I want that the details should be picked from the database and displayed in the pdf document.i am using report lab. Now have a look at the code

def pdf_view(request):
    response = HttpResponse(mimetype='application/pdf')
    response['Content-Disposition'] = 'attachment; filename=hello.pdf'
    p = canvas.Canvas(response)
    details = Data.objects.all()
    print details

    p.drawString(20, 800, details)
    p.drawString(30, 700, "I am a Python Django Professional.")
    p.showPage()
    p.save()
    return response

now as a learning example i have made two fields in models

class Data(models.Model):
    first_name = models.CharField(max_length =100,blank=True,null=True)
    last_name = models.CharField(max_length =100,blank=True,null=True)

    def __unicode__(self):
        return self.first_name

and i want that in the pdf document it should display the name s whatever i fill through admin but it is giving me error

'Data' object has no attribute 'decode'

Request Method:     GET
Request URL:    http://localhost:8000/view_pdf/
Django Version:     1.3
Exception Type:     AttributeError
Exception Value: 

i want to pik the details from the database and display in the pdf document

'Data' object has no attribute 'decode'

Upvotes: 2

Views: 1185

Answers (1)

Daniel Roseman
Daniel Roseman

Reputation: 599610

It would have helped if you'd posted the actual traceback.

However I expect the issue is this line:

p.drawString(20, 800, details)

Details is a queryset, that is a list-like container of model instances. It's not a string, and neither does it contain a string. Maybe you want something like:

detail_string = u", ".join(unicode(obj) for obj in details) 

which calls the __unicode__ method on every object in your queryset, and joins the resulting list with commas.

Upvotes: 5

Related Questions