python - 我正在尝试从用户输入的数据库中检索数据,结果将在 pdf 文件中
问题描述
我正在尝试从用户输入的数据库中检索数据,结果将在 pdf 文件中,但不幸的是没有成功。谁能帮我..
这是我第一次在 stackoverflow 上提问,即使我尝试添加我的代码,它也显示了一些错误。
代码在评论中
模型.py
class Doners(models.Model):
name = models.CharField(max_length=100)
desc = models.CharField(max_length=100)
视图.py
def donateddetails(request,*args, **kwargs):
data = {
'name' : {{Doners.name}},
'desc' : {{Doners.desc}}
}
pdf = render_to_pdf('donateddetails.html', data)
return HttpResponse(pdf, content_type='application/pdf')
实用程序.py
from io import BytesIO
from django.http import HttpResponse
from django.template.loader import get_template
from xhtml2pdf import pisa
def render_to_pdf(template_src, context_dict={}):
template = get_template(template_src)
html = template.render(context_dict)
result = BytesIO()
pdf = pisa.pisaDocument(BytesIO(html.encode("ISO-8859-1")), result)
if not pdf.err:
return HttpResponse(result.getvalue(),
content_type='application/pdf')
return None
解决方案
推荐阅读
- python-3.x - 如何使训练神经网络的特征向量大小相等?
- powershell - 从 powershell 中的环境变量获取内置帐户
- timeout - Typescript 虚拟助手的 botbuilder-timeout
- vb6 - 是否有允许 Active Reports 在 Win10 操作系统下运行的解决方法?
- java - 如何在两个 CanvasView 对象上使用 onClickListener?
- javascript - 如何在其网格内移动元素?
- debian - debmake - 安装对规则文件中指向的一个文件没有影响
- python - 我可以将函数作为类属性传递并调用它吗?
- python - 如何在 if 语句中使用 Python 对象
- nginx - 如何使带有斜杠和不带斜杠的 nginx 位置匹配路径?