【问题标题】:Why this function has a TypeError?为什么这个函数有一个 TypeError?
【发布时间】:2021-07-25 11:04:57
【问题描述】:

当我想将订单详细信息打印为 PDF 文件时,它会给出以下信息:

错误:

admin_order_pdf() got an unexpected keyword argument 'order'

功能:

@staff_member_required
def admin_order_pdf(request, order_id):
    order = get_object_or_404(Order, id=order_id)
    html = render_to_string('orders/pdf.html', {'order': order})
    response = HttpResponse(content_type='application/pdf')
    response['Content-Disposition'] = f'filename=order_{order.id}.pdf'
    weasyprint.HTML(string=html).write_pdf(response,
                                           stylesheets=[weasyprint.CSS(
                                               settings.STATIC_ROOT + 'css/pdf.css'
                                           )])
    return response

【问题讨论】:

  • 没有order 关键字。这是order_id。您没有显示您是如何调用该函数的,因此无法提供进一步的帮助

标签: python django web backend


【解决方案1】:

您需要接受 order 作为关键字参数:

def admin_order_pdf(request, order):

或根据函数的要求更改您的调用代码以传递order_id

resp = admin_order_pdf(request=req, order_id=123)

【讨论】:

    猜你喜欢
    • 2019-10-05
    • 2019-01-16
    • 1970-01-01
    • 2021-11-16
    • 1970-01-01
    • 1970-01-01
    • 2016-09-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多