【发布时间】:2021-11-16 17:17:10
【问题描述】:
我目前有一个应用程序,旨在根据用户设置生成文档。这些设置应该由程序根据类的实例设置读取,以便程序知道它正在为哪个设置生成文档。
在为用户创建实例设置以编辑每个模型条目时,代码的工作方式如下:
setting = get_object_or_404(SettingsClass, pk=setting_pk)
if request.method == 'GET':
form = SettingUpdateForm(instance=setting)
return render(request, 'main/viewSettings.html', {'setting': setting, 'form':form})
else:
form = SettingUpdateForm(request.POST, instance=setting)
if form.is_valid():
form.save()
return redirect('settingsHome')
所以它使用“get_object_or_404”函数返回用户应该使用的实例,并告诉表单在此处使用该实例:form = SettingUpdateForm(instance=setting)
但是,当我在读取模型时尝试执行此操作时,相同类型的设置不起作用。这是我目前在我的观点中设置的:
def printReports(request , reports_pk):
pkForm = get_object_or_404(SettingsClass , pk=reports_pk)
form= SettingsClass(instance=pkForm)
complexName = form.Complex
我基本上是想告诉程序在模型的某个实例中工作并从那里读取项目,例如:complexName = form.Complex
如果有人有任何解决方案或替代方法来设置这样的东西,请提供帮助。我将在下面添加我的 URL、模板和视图代码以便更好地查看
Views.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')
模板:
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' %}" class="list-group-item list-group-item-action" >{{ x.Complex }} Reports</a>
{% endfor %}
</div>
{% endblock %}
pdf-trialbalance.html:
{% 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 %}
【问题讨论】:
标签: python django django-views django-templates