我正在尝试将一些数据库值打印到HTML页面上。
html代码通过对描述值的数量进行计数的for循环来运行。
但是,它会打印每个借方、贷方和账号条目的整个数据库。
我很确定问题出在for循环结构中,请协助。
Home.html:
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-wEmeIV1mKuiNpC+IOBjI7aAzPcEZeedi5yW5f2yOq55WWLwNGmvvx4Um1vskeMj0" crossorigin="anonymous">
{% extends "main/base.html"%}
{% block content%}
<h1> Kyle Database </h1>
<h2>Trial Balance</h2>
<br>
<br>
<table>
<th>Account</th>
<th>Description</th>
<th>Debit</th>
<th>Credit</th>
{% for xAlls in xAll %}
<tr>
<td>{{ accountNo }}</td>
<td>{{ description }}</td>
<td>{{ debit }}</td>
<td>{{ credit }}</td>
</tr>
{% endfor %}
</table>
{% endblock %}Views.py:
def home(request):
return render(request , 'main/home.html')
def Kyletrb(request):
desc = "SELECT Description FROM [Kyle].[dbo].[_btblCbStatement] WHERE Account <> ''"
cursor = cnxn.cursor();
cursor.execute(desc);
description = [tup[0] for tup in cursor.fetchall()]
accNo = "SELECT Account FROM [Kyle].[dbo].[_btblCbStatement] WHERE Account <> ''"
cursor.execute(accNo);
accountNo = [tup[0] for tup in cursor.fetchall()]
deb = "SELECT Debit FROM [Kyle].[dbo].[_btblCbStatement] WHERE Account <> ''"
cursor.execute(deb);
debit = [tup[0] for tup in cursor.fetchall()]
cred = "SELECT Credit FROM [Kyle].[dbo].[_btblCbStatement] WHERE Account <> ''"
cursor.execute(cred);
credit = [tup[0] for tup in cursor.fetchall()]
all = "SELECT Description, Account ,Credit,Debit FROM [Kyle].[dbo].[_btblCbStatement] WHERE Account <> ''"
cursor.execute(all);
xAll = [tup[0] for tup in cursor.fetchall()]
return render(request , 'main/Kyletrb.html' , {"description":description , "accountNo":accountNo , "debit":debit , "credit":credit , "xAll":xAll})输出:

发布于 2021-08-26 09:19:28
我已经解决了格式化问题。
我没有使用表,而是对每一列使用了'div‘标记
.html:
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-wEmeIV1mKuiNpC+IOBjI7aAzPcEZeedi5yW5f2yOq55WWLwNGmvvx4Um1vskeMj0" crossorigin="anonymous">
{% extends "main/base.html"%}
{% block content%}
<h1>Kyle Database Trial Balance</h1>
<br>
<div class="container">
<div class="row mb-0">
<div class="col">
<h3>Account</h3>
{% for accountNo in accountNo %}
<p style="font-size:10px">{{ accountNo }}</p>
{% endfor %}
</div>
<div class="col-4">
<h3>Description</h3>
{% for description in description %}
<p style="font-size:10px">{{ description }}</p>
{% endfor %}
</div>
<div class="col">
<h3>Debit</h3>
{% for debit in debit %}
<p style="font-size:10px">{{ debit }}</p>
{% endfor %}
</div>
<div class="col">
<h3>Credit</h3>
{% for credit in credit %}
<p style="font-size:10px">{{ credit }}</p>
{% endfor %}
</div>
</div>
</div>
{% endblock %}输出:

发布于 2021-08-26 09:18:21
首先,您可以一次对所有数据进行简单的查询(就像您在上一次fetch中所做的那样)。我会做一个字典列表,如下所示:
def Kyletrb(request):
all = "SELECT Description, Account ,Credit,Debit FROM [Kyle].[dbo].[_btblCbStatement] WHERE Account <> ''"
cursor.execute(all);
xAll = cursor.fetchall()
cursor.close()
xAll_l = []
for row in xAll:
rdict = {}
rdict["Description"] = row[0]
rdict["Account"] = row[1]
rdict["Credit"] = row[2]
rdict["Debit"] = row[3]
xAll_l.append(rdict)
return render(request , 'main/Kyletrb.html' , {"xAlls":xAll_l}) 之后,您可以在模板中创建一个for循环:
<table>
<th>Account</th>
<th>Description</th>
<th>Debit</th>
<th>Credit</th>
{% for xAll in xAlls %}
<tr>
<td>{{ xAll.Description }}</td>
<td>{{ xAll.Account }}</td>
<td>{{ xAll.Debit }}</td>
<td>{{ xAll.Credit }}</td>
</tr>
{% endfor %}
</table>发布于 2021-08-26 07:15:10
尝试以下代码
{% for description in description %}
<tr>
<td>{{ description.accountNo }}</td>
<td>{{ description.description }}</td>
<td>{{ description.debit }}</td>
<td>{{ description.credit }}</td>
</tr>
{% endfor %}https://stackoverflow.com/questions/68934033
复制相似问题