您好我正在尝试为django oscar设置发票生成。 我找到了一个有用的链接https://groups.google.com/forum/#!topic/django-oscar/sg1qtyuu32Q (这个谷歌组的两个主要链接是 https://gist.github.com/elbaschid/8722203和https://gist.github.com/elbaschid/8776935)
但我在尝试设置时遇到了问题。
我按照链接中的建议保存了模板,下面是OrderListView下的代码:
def generate_packing_slips(self, request, orders):
template = loader.get_template(self.packing_slip_template)
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 = RequestContext(request, {
'order': order,
'STATIC_ROOT': settings.STATIC_ROOT,
'voucher_codes': voucher_codes,
})
html = template.render(context)
result = StringIO()
order_pdf = pisa.pisaDocument(StringIO(html.encode("UTF-8")), result)
if order_pdf.err:
messages.error(
self.request,
_("An problem occured trying to generate the packing slip for "
"order #%s") % order.number,
)
else:
main_pdf.addDocument(order_pdf)
response = HttpResponse(main_pdf.getvalue(), mimetype='application/pdf')
filename = self.get_packing_slip_filename(orders)
response['Content-Disposition'] = 'attachment; filename=%s' % filename
return response
然而,这给了我一个错误
AttributeError at /dashboard/orders/
'OrderListView' object has no attribute 'packing_slip_template'
模板的路径为'dashboard/orders/order_packing_slip.html'
我还在order_list.html上添加了一个按钮,添加以下代码:
<button style="margin-right:10px" type="submit" class="btn w3-black w3-hover-dark-gray" name="action" value="generate_packing_slips" data-loading-text="{% trans 'Submitting...' %}">{% trans "Download" %}</button>
如果您对解决方案有任何建议,我将不胜感激!
非常感谢