Form URL Error :Reverse for 'printReports' with no arguments not found. 1 pattern(s) tried

Form URL Error :Reverse for 'printReports' with no arguments not found. 1 pattern(s) tried

我目前 运行 在尝试访问我的报告主页时出现上述错误。代码为 href="{% url 'printReports' reports_pk %}"

的表单的 'HREF' 部分似乎有问题

模板、视图和 URL 列在下面的代码中:

reportsHome.html :

{% block content%}
<h1 style=" text-align: center">Reports</h1>
<hr>
 <br>
 <div class="list-group">
     <a href="#" class='list-group-item active'>Print Single Complex's</a>
{% for x in model %}
    <a href="{% url 'printReports' reports_pk %}" class="list-group-item list-group-item-action" >{{ x.Complex }} Reports</a>
{% endfor %}
</div>
{% endblock %}

printPDF.html :

<title>PDF Outuput - TrialBalance</title>

{% block content%}
<h1 class = 'center'>Kyle Database Trial Balance</h1>
<br>

</div>
<br>
<br>

<div class="table-container">
  <table style="width: 100%">
    <th >Account</th>
    <th>Description</th>
    <th>Debit</th>
    <th>Credit</th>
    {% for arr_trbYTD in arr_trbYTD %}
      <tr>
        <td>{{ arr_trbYTD.Description }}</td>
        <td>{{ arr_trbYTD.Account }}</td>
        <td>
        {%if arr_trbYTD.Debit > 0%}
            {{arr_trbYTD.Debit}}
        {%endif%}
        </td>
        <td>
        {%if arr_trbYTD.Credit > 0%}
          {{arr_trbYTD.Credit}}
        {%endif%}
        </td>
      </tr>
    <tr >
    {% endfor %}
    <td> <b>Totals</b> </td>
    <td> </td>
    {% for xDebitTotal in xDebitTotal %}
    <td><b>R {{ xDebitTotal }}</b></td>
    {% endfor %}

    {% for xCreditTotal in xCreditTotal %}
    <td><b>R {{ xCreditTotal }}</b></td>
    {% endfor %}

    </tr>
  </table>
</div>
<br>
<br>
<br>

{% endblock %}

Views.py :

def printReports(request , reports_pk):
    pkForm = get_object_or_404(SettingsClass , pk=reports_pk)
    form= SettingsClass(instance=pkForm)

    complexName = form.Complex

    printTrialBalance = True
    includeOpeningBalance = ''
    useMainAccounts = 'len(Master_Sub_Account) < 5 '
    printNullValues = ''   # else it will print null values
    printDescription = ''  # if false it wil remove the print description line
    printAccount = '' # if false it wil remove the print account line
    OrderByAccount = 'ORDER BY iAccountType '

    #CHECKING TRIAL BALANCE SETTINGS
    if form.Trial_balance_Year_to_date == True:
        printTrialBalance = True

        baseTRBYear = 'Inner JOIN [?].[dbo].[Accounts] '\
                     'on Accounts.AccountLink = genLedger.AccountLink '\
                     'Inner JOIN [?].[dbo].[_etblGLAccountTypes] as AccountTypes '\
                     'on Accounts.iAccountType = AccountTypes.idGLAccountType '\
                     'WHERE genLedger.AccountLink not in (161,162,163,164,165,166,167,168,122) '\
                     'AND genLedger.TxDate > ?'\

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

        #SQL STATEMENT
        xtrbYTD = baseSelect + trbYTD + op1 + op2 + op3 + op6

        cursor = cnxn.cursor();
        cursor.execute(baseTRBYear, [complexName], [complexName], [complexName], [one_yrs_ago]);
        xAll = cursor.fetchall()
        cursor.close()
        xtrbYTD = []
        for row in xtrbYTD:
            rdict = {}
            rdict["Description"] = row[0]
            rdict["Account"] = row[1]
            rdict["Debit"] = row[2]
            rdict["Credit"] = row[3]
            arr_trbYTD.append(rdict)

        content =  {"arr_trbYTD":arr_trbYTD , 'xCreditTotal':xCreditTotal , 'xDebitTotal':xDebitTotal , 'complexName':complexName , 'openingBalances': openingBalances ,'printZero':printZero}
        html_string=render_to_string('main/pdf-trialbalance.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:
        printTrialBalance = False

    return render(request , 'main/printReports.html')

URLS.PY:

  #Reports
    path('reportsHome' , views.reportsHome, name='reportsHome'),
    path('accConnect/printReports/<int:reports_pk>' , views.printReports , name='printReports')
]

如果有人知道可能导致此错误的原因,请提供帮助

我假设 reports_pk 在你的模型中,所以我正在编辑它希望它能按照我的理解工作

{% block content%}
    <h1 style=" text-align: center">Reports</h1>
    <hr>
     <br>
     <div class="list-group">
         <a href="#" class='list-group-item active'>Print Single Complex's</a>
    {% for x in model %}
        <a href="{% url 'printReports' /{{reports_pk}} %}" class="list-group-item list-group-item-action" >{{ x.Complex }} Reports</a>
    {% endfor %}
    </div>
    {% endblock %}

如果 'model' 表示您的模型和模型的 x 元素,那么您可以使用 x.pk

访问主键
{% block content%}
<h1 style=" text-align: center">Reports</h1>
<hr>
 <br>
 <div class="list-group">
     <a href="#" class='list-group-item active'>Print Single Complex</a>
{% for x in model %}
    <a href="{% url 'printReports' x.pk %}" class="list-group-item list-group-item-action" >{{ x.Complex }} Reports</a>
{% endfor %}
</div>

{% endblock %}