[Python] How to render a jinja html list with two lists using Python and Flask?

Discussão em 'Python' iniciado por Stack, Março 11, 2025.

  1. Stack

    Stack Membro Participativo

    I'm working on a dynamic table with some entries extracted from a database with a simple Python conection. I'm working with Flask and Jinja for the rendering of the HTML.

    For achieving this, I use a cur.fetchall():

    cur.execute('SELECT * FROM conceptos WHERE conceptos.conceptoTipo = 1')
    data = cur.fetchall()


    But, I'm interested on modifying an specific value from each entry, in this case the value on the 9th position.And, for that, I create a list and use a for to go through the entries in the data, like this:

    x = []
    for i in data:
    x.append(i[9]+1) #Just adding 1 to the numeric value for testing the change.


    And lastly, I return both variables with the render_template:

    return render_template('descuentosBeneficiarios.html', conceptos = data, operas = x)


    Inside the HTML, I use Jinja2 to expose the variables dynamically inside a simple table:

    {% for concepto in conceptos %}

    <tr>
    <!-- Recorrer Campos de Tabla-->
    <td>{{concepto.0}}</td>
    <td>{{operas}}</td>
    <td>{{concepto.2}}</td>
    <td>{{concepto.3}}</td>
    <td>{{concepto.4}}</td>
    <td>{{concepto.5}}</td>
    <td>{{concepto.6}}</td>
    <td>{{concepto.7}}</td>
    <td>{{concepto.8}}</td>
    <td>{{concepto.9}}</td>
    <td>{{concepto.10}}</td>
    <td>{{concepto.11}}</td>
    <td>
    </tr>
    {% endfor %}


    As a result, for each row I'm getting all the values inside the operas list on the second column, and I only need the first for the first row, the second for the second, and so on, but I don't know how to do that.

    Thanks for everything!

    Continue reading...

Compartilhe esta Página