python - 表单 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')
]
如果有人知道可能导致此错误的原因,请协助
解决方案
我假设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 %}
推荐阅读
- javascript - React nodemailer net.isIP 不是一个函数
- zend-framework - 了解zend中的网格布局
- apache-nifi - 如何在 apache nifi 中连接 putHQL
- reactjs - 道具中的反应样式
- r - rollapply 中的“align”参数有什么作用?
- python - Django Crontab:如何停止并行执行
- c# - 在 Xamarin.Forms Web 视图中打开本地 HTML 文件而不是在资产文件夹中
- android - Android 房间迁移更改列数据类型而不会丢失数据
- node.js - 在 webpack 运行时读取 process.env
- elasticsearch - CloudWatch 到 Elastic Cloud:缺少数据?