For loops in HTML Tables (for var in var)

For loops in HTML Tables (for var in var)

我正在尝试将一些数据库值打印到 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})

输出:

试试这个代码

{% for description in description %}
  <tr>
   <td>{{ description.accountNo }}</td>
   <td>{{ description.description }}</td>
   <td>{{ description.debit }}</td>
   <td>{{ description.credit }}</td>
  </tr>
  {% endfor %}

您在 for 循环中使用了相同的变量名两次。

通常是

for(description in descriptions)

注意数量差异。

您可以将 for 循环替换为以下代码。我希望它能奏效。

{% for desc in description %}
  <tr>
   <td>{{ desc.accountNo }}</td>
   <td>{{ desc.description }}</td>
   <td>{{ desc.debit }}</td>
   <td>{{ desc.credit }}</td>
  </tr>
{% endfor %}

并确保您具有与 HTML 模板中给定的模型相同的对象

Please try as below.

{% for description in descriptions %}
     <tr>
         <td>{{ description.accountNo }}</td>
         <td>{{ description.description }}</td>
         <td>{{ description.debit }}</td>
         <td>{{ description.credit }}</td>
     </tr>
{% endfor %}

首先,您可以一次对所有数据进行简单查询(就像您在上次获取中所做的那样)。 我会列出这样的字典:

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>

我已经弄清楚格式问题了。

我没有使用 table,而是为每一列使用了 '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 %}

输出: