简体   繁体   English

在表格 html 模板中显示使用 DJANGO 查询 SQL 的结果

[英]Show in a table html template the result of a SQL query using DJANGO

I have DDBB with a specific table that is the result of a stored procedure calculation.我有一个带有特定表的 DDBB,它是存储过程计算的结果。 I would like to show this SQL table in a table in html using a DJANGO Template.我想使用 DJANGO 模板在 html 的表中显示此 SQL 表。

I have tried in many different ways but the nearest I got was show only one register.我尝试了许多不同的方式,但我得到的最接近的是只显示一个寄存器。

views.py视图.py

cursor.execute("SELECT NIVEL, COMP,  PONDERACION FROM COMP ORDER BY NIVEL ")
COMP = cursor.fetchall()[0]

index.html:索引.html:

<table>
    <tr>
        <th>NIVEL</th>
        <th>COMPONENTE</th>
        <th>PONDERACION</th>
    </tr>
    {% for row0 in COMP %}

    <td>{{ row0 }}</td>
    {% endfor %}
    </tr>
</table>

I know that a query is a tuple but I don't know how to transform a tuple with many registers in a table just like the result of a SQL query...我知道查询是一个元组,但我不知道如何转换一个表中包含许多寄存器的元组,就像 SQL 查询的结果一样......

Thank you谢谢

I got it.我知道了。 views.py视图.py

cursor.execute("SELECT NIVEL, COMP,  PONDERACION FROM COMP ORDER BY NIVEL ")
COMP = cursor.fetchall()

html: html:

<table>
                    <tr> 
                        <th>NIVEL</th>
                        <th>COMPONENTE</th> 
                        <th>PONDERACION</th> 
                    </tr>
                        {% for row0 in COMP %} 
                    <tr>                        
                        <td>{{ row0.0 }}</td>
                        <td>{{ row0.1 }}</td>
                        <td>{{ row0.2 }}</td>
                            {% endfor %}
                    </tr>
        </table>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM