I am currently running into the above error when trying to access my reportsHome page. It seems to be a problem with the 'HREF' section of the form where the code is href="{% url 'printReports' reports_pk %}"
The templates , views and URLs are listed in the below code:
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')
]
If anyone knows what could be causing this error , please assist
CodePudding user response:
I am assuming that reports_pk is in your model so i am editing accoring it hope it works as my understanding
{% 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 %}
CodePudding user response:
if 'model' represents your model and x element of your model then you can access the primary key using 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 %}