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

2024-03-14

我猜我的 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

您的变量 x 不会返回任何可以打印到 PDF 中的内容,因为它是一个查询集,而不是相互附加的几个字符串。我刚刚开始使用 Django 并获取值的工作方式如下:

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

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

有关更多信息,我只能建议阅读有关编写查询的教程 https://docs.djangoproject.com/en/1.10/topics/db/queries/#limiting-querysetshttp://在 django 网站上。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

我如何在数据库中显示我的数据并将其导出为 pdf -Django 的相关文章

随机推荐