2011-06-21 40 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

非常感謝你的工作 –