开发者

django output pdf

开发者 https://www.devze.com 2023-03-14 14:06 出处:网络
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

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 b开发者_高级运维ut 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'


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消