Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 通过电子邮件发送HTML生成的PDF:应为类似object的字节,而不是HttpResponse_Python_Django - Fatal编程技术网

Python 通过电子邮件发送HTML生成的PDF:应为类似object的字节,而不是HttpResponse

Python 通过电子邮件发送HTML生成的PDF:应为类似object的字节,而不是HttpResponse,python,django,Python,Django,我正试图通过电子邮件发送一份由HTML模板生成的PDF 我得到以下错误:预期的字节类似于对象,而不是HttpResponse 生成\u pdf: def generate_pdf(template_src, context_dict={}): template = get_template(template_src) html = template.render(context_dict) result = BytesIO() pdf = pisa.pisaDocume

我正试图通过电子邮件发送一份由HTML模板生成的PDF

我得到以下错误:
预期的字节类似于对象,而不是HttpResponse

生成\u pdf

def generate_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
查看pdf电子邮件:

def pdfView(request):

   data = {'test':'test',
              'mylist': 'test'
              }
   pdf = generate_pdf('main/test.html', data)
   msg = EmailMessage("title", "content", to=["email@email.com"])
   msg.attach('my_pdf.pdf', pdf, 'application/pdf')
   msg.content_subtype = "html"
   msg.send()

   return HttpResponse(pdf, content_type='application/pdf')

您正在函数中返回一个
HttpResponse

只需更改
返回HttpResponse(result.getvalue(),content\u type='application/pdf')

返回result.getvalue()