社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
Py学习  »  Django

Django HTML模板表呈现

Jason Liu • 4 年前 • 935 次点击  

我想把一张桌子渲染成这样:

<table>
  <tr>
    <td>some data</td>
    <th>special one</th>
    <td>some data</td>
      ...
    <td>some data</td>
  </tr>
  ...
</table>

有一个 solution

<table>
  {% for rowval in results %}    
     <tr>
     {% for val in rowval %}
      <td>{{val}}</td>
     {% endfor %}
    </tr>
  {% endfor %}
</table> 

但在我看来 th 如果有记录,则排在第二位。

还有一个 solution 不如下面的答案保留部分表, td tr 在视野中。

有办法实现这个功能吗?

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/56752
 
935 次点击  
文章 [ 1 ]  |  最新文章 4 年前
Iain Shelvington
Reply   •   1 楼
Iain Shelvington    4 年前

django模板中有一些可用的变量 for loop ,其中一个被命名为 forloop.counter 它给出了循环的当前迭代。可以使用此变量在第二个循环中呈现不同的内容

<table>
  {% for rowval in results %}    
    <tr>
      {% for val in rowval %}
        {% if forloop.counter == 2 %}
          <th>{{ val }}</th>
        {% else %}
          <td>{{ val }}</td>
        {% endif %}
     {% endfor %}
    </tr>
  {% endfor %}
</table>