首页 > 解决方案 > 表单 URL 错误:未找到任何参数的“printReports”反向。已尝试 1 种模式

问题描述

我目前在尝试访问我的报告主页时遇到了上述错误。代码所在表单的“HREF”部分似乎有问题href="{% url 'printReports' reports_pk %}"

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

报告首页.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 %}

打印PDF.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 %}

视图.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')
]

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

标签: pythondjangodjango-viewsinstance

解决方案


我假设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 %}

推荐阅读