【发布时间】:2021-10-26 05:59:36
【问题描述】:
我正在尝试将一些数据库值打印到 HTML 页面上。
html 代码通过一个计算描述值数量的 for 循环运行。
但是,它会为借方、贷方和帐号的每个条目打印整个数据库。
我很确定问题出在 for 循环结构内部,请协助。
主页.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})
【问题讨论】:
-
您忘记发布您的 Python 代码了吗?
-
是的,请向您展示视图和模型。
-
@lewis ,我已经添加了视图,模型是空的
标签: python html django for-loop html-table