我正在尝试使用reportlabs生成pdf,但遇到The view APP.VIEW didn't 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)
答案 0 :(得分:2)
您没有从视图中返回任何。您不能只是致电render_to_pdf
或HttpResponse
- 您实际上必须返回结果。