简体   繁体   中英

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. I would like to show this SQL table in a table in html using a DJANGO Template.

I have tried in many different ways but the nearest I got was show only one register.

views.py

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

index.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...

Thank you

I got it. views.py

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

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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