Django reportlabs 没有返回 HttpResponse 对象
我正在尝试使用 reportlabs 生成 pdf,但遇到 The view APP.VIEW did not return an HttpResponse object.
错误。
函数和视图运行时没有任何异常,即使是 return HttpResponse(result.getvalue(), mimetype='application/pdf')
行也是如此。但我不断收到错误。
下面是我的代码:
def render_to_pdf(template_src, context_dict):
"""Function to render html template into a pdf file"""
template = get_template(template_src)
context = Context(context_dict)
html = template.render(context)
result = StringIO.StringIO()
pdf = pisa.pisaDocument(StringIO.StringIO(html.encode("UTF-8")), result)
if not pdf.err:
return HttpResponse(result.getvalue(), mimetype='application/pdf')
return HttpResponse('We had some errors<pre>%s</pre>' % escape(html))
视图:
def invoice_pdf(request, inv_no):
try:
inv = BC_Invoice.objects.select_related().get(invoice_no=inv_no)
render_to_pdf('bc_invoice_pdf.html', {'pagesize': 'A4',
'inv': inv}
)
except Exception, e:
pass
HttpResponse(None)
I am trying to generate a pdf using reportlabs and I am encountering a The view APP.VIEW didn't return an HttpResponse object.
error.
The function and view runs without any exceptions, even the line return HttpResponse(result.getvalue(), mimetype='application/pdf')
. But I keep getting the error.
Below is my code:
def render_to_pdf(template_src, context_dict):
"""Function to render html template into a pdf file"""
template = get_template(template_src)
context = Context(context_dict)
html = template.render(context)
result = StringIO.StringIO()
pdf = pisa.pisaDocument(StringIO.StringIO(html.encode("UTF-8")), result)
if not pdf.err:
return HttpResponse(result.getvalue(), mimetype='application/pdf')
return HttpResponse('We had some errors<pre>%s</pre>' % escape(html))
The view:
def invoice_pdf(request, inv_no):
try:
inv = BC_Invoice.objects.select_related().get(invoice_no=inv_no)
render_to_pdf('bc_invoice_pdf.html', {'pagesize': 'A4',
'inv': inv}
)
except Exception, e:
pass
HttpResponse(None)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您不会从视图中返回任何内容。您不能只调用
render_to_pdf
或HttpResponse
- 您实际上必须返回结果。You aren't returning anything from the view. You can't just call
render_to_pdf
orHttpResponse
- you actually have to return the result.