You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Django视图报错trialBalanceMonthly()收到多个TBMreports_pk参数如何解决

问题根源

Django视图函数强制要求第一个参数为请求对象request,你当前的trialBalanceMonthly视图没有声明该参数,Django调用时会自动将request作为第一个位置参数传入,同时路径捕获的TBMreports_pk会作为关键字参数传入,相当于实际传入的参数为(request, TBMreports_pk=xxx),但函数仅声明接收1个参数,最终触发参数重复传入的报错。

修复方案

  1. 修正所有报表视图的参数定义,第一个位置补充request参数,同时补全缺失的返回值:
# 月度试算平衡表视图修改示例,其余三个视图按相同逻辑修改
def trialBalanceMonthly(request, TBMreports_pk):
    pkForm = get_object_or_404(SettingsClass, pk=TBMreports_pk)
    complexName = pkForm.Complex

    ### Printing Trial Balance PDF
    response = HttpResponse(content_type='application/pdf')
    response['Content-Disposition'] = 'attachment; filename=TrialBalanceMonthly' + \
                                      str(datetime.now()) + '.pdf'
    response['Content-Transfer-Encoding'] = 'binary'

    content = {'printDescM': printDescM, 'printAccM': printAccM, "xtrbMonth": xtrbMonth,
               'xCreditTotalM': xCreditTotalM, 'xDebitTotalM': xDebitTotalM, 'complexName': complexName,
               'printZeroM': printZeroM}
    html_string = render_to_string('main/reports/trialBalanceMonthly.html', content)
    html = HTML(string=html_string)
    result = html.write_pdf()

    with tempfile.NamedTemporaryFile(delete=True) as output:
        output.write(result)
        output.flush()
        output.seek(0)
        response.write(output.read())
    # 补充返回响应
    return response
else:
    # 补充返回重定向
    return redirect('main/reportsHome')
  1. 其余三个视图同步修改参数定义:
  • def trialBalanceYearly(request, TBYreports_pk):
  • def incomeStatementMonthly(request, ISMreports_pk):
  • def incomeStatementYearly(request, ISYreports_pk):

内容的提问来源于stack exchange,提问作者KyleStranger

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.30 04:48:04