I am currently trying to get an instance of my model to read from it and print reports based on the model
However when trying to reach the URL I get the above error, I assume that the instance variable doesn't work but I don't know why.
I have been reading other articles and found that others get this error when they use Form instead of ModelForm, so I don't know if this could be the same thing as that.
Please see the below code :
Model.py:
class SettingsClass(models.Model):
Complex = models.CharField(choices=complex_list , max_length = 22 ,default='1' , unique=True)
Trial_balance_Year_to_date= models.BooleanField(default = False)
tbytd_Include_opening_balances=models.BooleanField(default = False)
tbytd_Only_use_main_accounts=models.BooleanField(default = False)
tbytd_Print_null_values=models.BooleanField(default = False)
tbytd_Print_description=models.BooleanField(default = True)
tbytd_Print_account=models.BooleanField(default = True)
tbytd_Sort_by_account_name=models.BooleanField(default = True)
def __str__(self):
return (self.Complex ' Settings')
View.py::
def reportsHome(request):
model = SettingsClass.objects.all().order_by('Complex')
content ={'model':model }
return render(request, 'main/reportsHome.html' , content)
def printReports(request , reports_pk):
pkForm = get_object_or_404(SettingsClass , pk=reports_pk)
form= SettingsClass(instance=pkForm)
complexName = form.Complex
#CHECKING TRIAL BALANCE SETTINGS
if form.Trial_balance_Year_to_date == True:
printTrialBalanceYTD = True
### 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
baseSelect = 'SELECT ' op5 op4 ' Debit , Credit FROM [?].[dbo].[PostGL] AS genLedger '
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:
printTrialBalanceYTD = False
Urls.py:
#Reports
path('accConnect' , views.reportsHome, name='reportsHome'),
path('accConnect/printReports/<int:reports_pk>' , views.printReports , name='printReports')
Template.html:
{% extends "main/base.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' %}" class="list-group-item list-group-item-action" >{{ x.Complex }} Reports</a>
{% endfor %}
</div>
{% endblock %}
CodePudding user response:
Here, pkForm is already an instance of the model SettingsClass
pkForm = get_object_or_404(SettingsClass , pk=reports_pk)
Thus, no need to perform this which is also incorrect since SettingsClass is a model and it doesn't have a field with the name instance (its fields are Complex, Trial_balance_Year_to_date, etc.).
form= SettingsClass(instance=pkForm)
Perhaps what you meant to use was the form class instead of that model class SettingsClass. But based on the way you used this variable form, seems like you could just remove it and substitute pkForm directly.
