首页 > 解决方案 > 我正在尝试从用户输入的数据库中检索数据,结果将在 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

标签: pythondjango-3.0

解决方案


推荐阅读