Configure HTML table using python’s Django for structure

Asked

Viewed 543 times

0

I’m making a table through 3 lists generated in the views, but I can’t leave the table configured correction in html, would be 3 columns and 60 lines, but they are not adjusted correctly, follow my HTML code:

       <table>
            <tr>
                <td>Horario</td>
                <td>Ligado</td>
                <td>Desligado</td>
            </tr>
            {% for x in teste4 %}
            <tr>
                <td>{{ x }}</td>
            </tr>
            {% endfor %}

            {% for y in tabela1 %}
            <tr>
                <td>{{ y }}</td>
            </tr>
            {% endfor %}
            {% for z in tabela2 %}
            <tr>
                <td>{{ z }}<td>
            </tr>

            {% endfor %}


        </table>

Generates this result (decreases the size to fit the image)

inserir a descrição da imagem aqui

And the code that generates this table:

    x = PrettyTable(["Horario", "Ligado", "Desligado"])
    x.align["Horario"] = "2"
    x.align["Ligado"] = "2"
    x.align["Desligado"] = "r"
    x.padding_width = 1
    z=0
    while z < len(teste4):
        x.add_row([teste4[z], tabela1[z], tabela2[z]])
        z = z + 1
    print (x)

EDIT

As mentioned in the comments I changed the views to pass a method and the generated table also went wrong, follow code and image

    x = PrettyTable(["Horario", "Ligado", "Desligado"])
    x.align["Horario"] = "2"
    x.align["Ligado"] = "2"
    x.align["Desligado"] = "r"
    x.padding_width = 1
    z=0
    while z < len(teste4):
        x.add_row([teste4[z], tabela1[z], tabela2[z]])
        z = z + 1
    print (x)
    passar = x.get_html_string()

with the html being Table:{{ pass }}

inserir a descrição da imagem aqui

  • The structure of your table in HTML is quite wrong. You are inserting elements <tr> within elements <td>, who are already inside <tr>. In the table, you always define one row at a time, that is, for each <tr> you will have one or more <td> and this element can’t had <tr> within.

  • Yes I have done with the right structure, but the result comes out worse than this in the question, as incredible as it looks with this structure is where I got the result more like what I want, but I arranged the question with the right structure

  • As commented on in your other question, just use the method get_html_string of PrettyTable.

  • Until now I did not understand how to use this method

  • He returns a string with all the HTML code of your table, just display.

  • Out of curiosity, why do you have two accounts, this and this, on the site? Created a new by mistake?

  • tried here and returned wrong, I will edit the question with the method and your return, on the two accounts, I use one on each pc, when I created one did not remember that I had in the other, I spent time not using

  • And if you do {{ passar|safe }}?

  • opa, ai worked out, thank you, just a doubt, has as I leave the fields of the table more spaced between them?

Show 4 more comments

1 answer

2


As already mentioned, the easiest method is to use get_html_string of PrettyTable, because it already returns, as string, all the HTML code of your table.

However, by default, Django escapes the values injected into templates as a way to prevent some types of attack, such as XSS. To be able to display your table correctly, it will be necessary to indicate that the injected value is safe doing, in your template:

{{ passar|safe }}

For more details of the filter safe, see the documentation.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.