如何在数据库中显示我的数据并将其导出为 pdf -Django [英] How can i display my data in database and export it to pdf -Django

查看:20
本文介绍了如何在数据库中显示我的数据并将其导出为 pdf -Django的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

my x variable is getting all the data in my database, i guess? someone help me how can i display all data and export it to pdf file.

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 并且获取值的工作方式如下:

Your variable x won't return anything you can print into the PDF, because its a queryset and not a couple strings attached to each other. I just started working with Django and getting the values works something like this:

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

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

This code snippet will assign the vale of the row name of the first entry in your Item table to the variable x.

有关更多信息,我只能推荐阅读 有关在 django 网站上编写查询的教程.

For more Information I can only recommend reading the Tutorial about writing queries on the django website.

这篇关于如何在数据库中显示我的数据并将其导出为 pdf -Django的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆