我嘗試在管理面板中添加通過 PDF 查看訂單的選項(xiàng),但收到錯誤,我不知道原因也不知道如何修復(fù)它。這就是我一步一步做的?,F(xiàn)在的問題是,我已嘗試重新啟動所有內(nèi)容,但仍然收到錯誤消息。$ pip install weasyprint django-import-export然后我將其添加到已安裝的應(yīng)用程序中'import_export', 這是 admin.pydef order_pdf(obj): return mark_safe('<a href="{}">PDF</a>'.format(reverse('orders:admin_order_pdf', args=[obj.id])))order_pdf.short_description = 'Order PDF'class OrderAdmin(ImportExportActionModelAdmin): list_display = ['id', order_pdf]這是views.py@staff_member_requireddef admin_order_pdf(request,order_id): Order = get_object_or_404(order,id=order_id) html = render_to_string('order/pdf.html',{'order':Order}) response = HttpResponse(content_type='application/pdf') response['Content-Disposition'] = 'filename="order_{}.pdf"'.format(Order.id) # weasyprint.HTML(string=html).write_pdf(response,stylesheets=[weasyprint.CSS(settings.STATIC_ROOT + 'css/pdf.css')]) weasyprint.HTML(string=html).write_pdf(response,stylesheets=[weasyprint.CSS(settings.STATIC_ROOT)]) return response這是 url.py path('admin/order/(?P<order_id>\d+)/pdf/$', views.admin_order_pdf, name='admin_order_pdf')
1 回答

小怪獸愛吃肉
TA貢獻(xiàn)1852條經(jīng)驗(yàn) 獲得超1個贊
weasyprint 或 django-import-export 依賴于 libcairo 是一個共享庫。有些 python 包會自動編譯所有依賴項(xiàng),但情況并非如此。如果您不想自己弄清楚如何安裝 libcairo,我建議您使用 Anaconda,它在 Windows 上安裝依賴項(xiàng)方面做得很好。
https://www.anaconda.com/products/individual
我驗(yàn)證了 Anaconda 具有 cairocffi 包,它是觸發(fā)錯誤的 libcairo 周圍的 python 包裝器。
https://anaconda.org/conda-forge/cairocffi
添加回答
舉報(bào)
0/150
提交
取消