django-oscar发票文件名为ordernumber

时间:2017-12-18 21:04:03

标签: filenames invoice django-oscar

关注我的问题:django oscar invoice pdf generation

我终于设法让它发挥作用了。

我必须在python 3版本中下载xhtml2pdf并添加

    if isinstance(dest, BytesIO):
        data = data.encode("utf-8")

然后我添加了

def generate_packing_slips(self, request, orders):
    template = get_template('dashboard/orders/order_packing_slip.html')
    main_pdf = pisaPDF()

    for order in orders:
        voucher_codes = []
        for discount in order.discounts.all():
            if discount.voucher_code:
                voucher_codes.append(discount.voucher_code)

        context = {
            'order': order,
            'STATIC_ROOT': settings.STATIC_ROOT,
            'voucher_codes': voucher_codes,
        }
        html = template.render(context)
        result = BytesIO()

        order_pdf = pisa.pisaDocument(BytesIO(html.encode("UTF-8")), result)

        if not order_pdf.err:
            main_pdf.addDocument(order_pdf)
            response = HttpResponse(result.getvalue(), content_type='application/pdf')
            filename = "orderinvoice.pdf"
            response['Content-Disposition'] = 'attachment; filename=%s' % filename
            return response

一切正常,我可以按照上述模板下载发票。但是,我试图弄清楚如何将文件名作为订单号。 按照上面的代码,所有发票都将下载为orderinvoice.pdf。设置它的最佳方法是什么,文件名是(ordernumber).pdf?

非常感谢!

0 个答案:

没有答案