【问题标题】:For loops in HTML Tables (for var in var)HTML 表中的 for 循环(for var in var)
【发布时间】: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


【解决方案1】:

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

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>

【讨论】:

    【解决方案2】:

    试试这个代码

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

    【讨论】:

    • 上面的代码根本不打印代码
    • 然后使用其他变量名,例如 {% for des in description %} 并调用 des.accountNo 。如果这不起作用控制台结果并根据我的理解检查它,描述中不包含 accountNo 和其他属性。需要检查数据库查询和数据库结构。
    • 嗨,我已经这样做了。它现在根本不打印值。我已经修改了我的问题中的 home.html 代码,以便您可以看到结构
    • 好的。请看一下结构并尝试理解它。根据您的 Views.py:您已获取所有描述并通过 description = [tup[0] for tup in cursor.fetchall()] 将其粘贴到描述中,因此当您在 html 中应用该循环时,它仅适用于描述而不适用于其他属性。我的建议是制作一个 json,然后在 html 中获取所有需要的信息,或者像我们使用 for array 一样对数字使用 for 循环。我希望你明白我的意思。
    • 如果您执行 {% for acc in accountNo %},则仅适用于帐号。 {% for dcard in debit %} 仅用于借记卡。您需要合并所有数据或为每个属性使用单独的循环
    【解决方案3】:

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

    正常情况下

    for(description in descriptions)
    

    注意复数的不同。

    【讨论】:

    • 当我改变它时,它也会打印每个表条目中的所有值作为描述。
    【解决方案4】:

    您可以用以下代码替换您的 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 模板中给出了相同的模型对象

    【讨论】:

    • 我已经尝试过了,请查看带有输出和 home.html 代码的修正问题
    • desc = "SELECT Description FROM [Kyle].[dbo].[_btblCbStatement] WHERE Account ''"
    • 我认为这个 db 命令没有返回任何数据。
    • 嗨,如果我使用
      comman,db 命令可以工作,但是这不允许打印试算表的正确布局
    【解决方案5】:
    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 %}
    

    【讨论】:

      【解决方案6】:

      我已经解决了格式问题。

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

      输出:

      【讨论】:

        猜你喜欢
        • 2011-08-08
        • 1970-01-01
        • 2015-08-28
        • 2018-11-28
        • 1970-01-01
        • 2018-05-02
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多