2

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'

1 Answer 1

5

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.