【问题标题】:Adding values to cells in a specific column in HTML向 HTML 中特定列中的单元格添加值
【发布时间】:2023-01-05 23:45:22
【问题描述】:

我正在为我的 Django 项目创建一个非常基本的 HTML 表格。 我从 Here 那里得到了一些帮助,学习如何将列表从我的 Django 应用程序传递到我的 HTML 并为我的值生成行。 这次我的问题是:我如何才能将列表的第二列、第三列或任何特定列作为目标?例如,假设我有一个值列表并想将它们全部添加到我的表的第 3 列。 这是我尝试使用的代码,我想将 {{ name }} 值添加到第二列,但它不断将单元格添加到第一列


<html>
   <body>   
           <table border="1" cellpadding = "5" cellspacing="5"> 
               <tr>
                   <th> IDs </th>
                   <th> Names </th>
                   <th> Heights </th>
                 </tr>
               {% for id in myIDs %}
                   <tr> 
                      <td>{{ id }}</td>
                      
                   </tr>
               {% endfor %}
               <tr>
                   {% for name in myNames %}
                   <td>
                       <tr> 
                           <td>{{ name }}</td>      
                        </tr>
                   </td>    
                       {% endfor %}
               </tr>
               </table>           
   </body>
</html>

【问题讨论】:

    标签: python html django web web-applications


    【解决方案1】:

    对于每个 id、name 和 height,创建一个元组。

    然后创建一个空列表并将元组附加到数据。

    然后将数据作为上下文发送

    temp = (id, name, height)
    data = []
    data.append(temp)
    return render(request, 'index.html', {data: data})
    

    现在在html页面

    {% for id, name, height %}
    <tr>
        <td>{{id}}</td>
        <td>{{name}}</td>
        <td>{{height}}</td>
    </tr>
    {% endfor %}

    【讨论】:

      猜你喜欢
      • 2020-06-21
      • 1970-01-01
      • 2013-04-06
      • 1970-01-01
      • 2018-03-26
      • 2021-06-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多