如何在数据库中显示我的数据并将其导出到pdf -Django

时间:2016-10-16 15:35:40

标签: python django django-models django-views

我的x变量正在获取我数据库中的所有数据,我想?有人帮我如何显示所有数据并将其导出为pdf文件。

response = HttpResponse(content_type='application/pdf')
response['Content-Disposition'] = 'attachment; filename="WishList.pdf"'

buffer = BytesIO()

# Create the PDF object, using the BytesIO object as its "file."
p = canvas.Canvas(buffer)
x = Item.objects.all()

p.drawString(100, 100,x)

p.drawString(200,300,"sad")

# Close the PDF object cleanly.
p.showPage()
p.save()

# Get the value of the BytesIO buffer and write it to the response.
pdf = buffer.getvalue()
buffer.close()
response.write(pdf)
return response

1 个答案:

答案 0 :(得分:0)

您的变量x不会返回任何可以打印到PDF中的内容,因为它是一个查询集而不是彼此相连的几个字符串。我刚刚开始使用Django并且获得的值类似于:

x = Item.objects.all[0].name

此代码段会将Item表中第一个条目的行名称的值分配给变量x。

有关详细信息,我只建议您阅读django网站上的Tutorial about writing queries

相关问题