2011-06-21 46 views
2

大家好,因为我在Django的学习阶段,所以支持我。 我必须在django中生成pdf报告。我希望细节应该从数据库中挑选出来并显示在pdf文档中。我正在使用报告实验室。 现在看看代码django输出pdf

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 
现在

为学习的榜样我已经在模型

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 

做了两个领域,我想,在PDF文档中应该显示名S不管我通过填写管理员,但它给我的错误

'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: 

我想PIK PDF文档中从数据库和显示细节

'Data' object has no attribute 'decode' 

回答

4

如果你发布了实际的回溯信息,它会有所帮助。

但是我希望这个问题是这样的一行:

p.drawString(20, 800, details) 

细节是一个QuerySet,这是模型实例的列表状容器。它不是一个字符串,也不包含一个字符串。也许你想是这样的:

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

它调用了您的查询集的每一个对象在__unicode__方法,并加入用逗号结果列表。

+0

非常感谢你的工作 –