Django reportlabs没有返回HttpResponse对象

时间:2021-08-12 20:23:58

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.

我正在尝试使用reportlabs生成一个pdf,并且遇到了一个视图应用程序。view没有返回一个HttpResponse对象。错误。

The function and view runs without any exceptions, even the line return HttpResponse(result.getvalue(), mimetype='application/pdf'). But I keep getting the error.

函数和视图运行时没有任何异常,甚至连行都返回HttpResponse(result.getvalue(), mimetype='application/pdf')。但我还是得到了误差。

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)

1 个解决方案

#1


2  

You aren't returning anything from the view. You can't just call render_to_pdf or HttpResponse - you actually have to return the result.

你不会从视图返回任何东西。您不能只调用render_to_pdf或HttpResponse—您实际上必须返回结果。

#1


2  

You aren't returning anything from the view. You can't just call render_to_pdf or HttpResponse - you actually have to return the result.

你不会从视图返回任何东西。您不能只调用render_to_pdf或HttpResponse—您实际上必须返回结果。