首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >HTML表中的For循环(for var in var)

HTML表中的For循环(for var in var)
EN

Stack Overflow用户
提问于 2021-08-26 07:10:05
回答 6查看 70关注 0票数 1

我正在尝试将一些数据库值打印到HTML页面上。

html代码通过对描述值的数量进行计数的for循环来运行。

但是,它会打印每个借方、贷方和账号条目的整个数据库。

我很确定问题出在for循环结构中,请协助。

Home.html:

代码语言:javascript
复制
<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:

代码语言:javascript
复制
 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})

输出:

EN

回答 6

Stack Overflow用户

回答已采纳

发布于 2021-08-26 09:19:28

我已经解决了格式化问题。

我没有使用表,而是对每一列使用了'div‘标记

.html:

代码语言:javascript
复制
<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 %}

输出:

票数 0
EN

Stack Overflow用户

发布于 2021-08-26 09:18:21

首先,您可以一次对所有数据进行简单的查询(就像您在上一次fetch中所做的那样)。我会做一个字典列表,如下所示:

代码语言:javascript
复制
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循环:

代码语言:javascript
复制
<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>
票数 1
EN

Stack Overflow用户

发布于 2021-08-26 07:15:10

尝试以下代码

代码语言:javascript
复制
{% for description in description %}
  <tr>
   <td>{{ description.accountNo }}</td>
   <td>{{ description.description }}</td>
   <td>{{ description.debit }}</td>
   <td>{{ description.credit }}</td>
  </tr>
  {% endfor %}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68934033

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档