Search code examples
pythondjangodjango-tables2

Django - Best way to create tables


I am displaying data from my database following a certain pattern.

To do so, I'm using a template with {% for %} loop to create the lines / columns of the table.

  • First question: I'm not sure is this is the right way to do this and if there is a more efficient way.

  • Second question: How to sort. I would like to know how I could sort the table. Because of the {% for %} loop building the table, I don't know how to properly sort my table columns by clicking on the headers. I've seen many solutions but none would work with the loop in the table missing things up.

Indeed, with things like django-table2, I don't know how if I can incorporate my specific loop (instead of printing the entire table, which I can sort using django-table2 but w/o being capable of adapting both design and information displayed.

Below an example of how I'm building things in the template - with a table included in a table:

        <table>
          <thead>
            <tr>
              <th>Status</th>
              <th>Some</th>
              <th>Something</th>
              <th>Other</th>
            </tr>
          </thead>
          <tbody>

            {% block content %}
            {% for x in xx %}
                <tr>
                    <td> {{ x.status }} </td>
                    <td> {{ x.some}} </td>
                    <td> {{ x.something }} </td>
                    <td>

                        <table>
                            <thead>
                              <th> Br </th>
                              <th> Sc </th>
                              <th> Ic </th>
                             </thead>
                             <tbody>
                             <tr>
                                <td>{% if itemA|get_item:varX.name >= 1 %} Good {% else %} Bad {% endif %} </td>
                                <td>{% if itemB|get_item:varY.name >= 1 %} Good {% else %} Bad {% endif %} </td>
                                <td>{% if itemC|get_item:varY.name >= 1 %} Good {% else %} Bad {% endif %} </td>
                             </tr>
                         </table>

                     </td>
                </tr>
         </table>
                </td>
                </tr>

            {% endfor %}
            {% endblock %}

Many thanks in advance for your help !


Solution

  • First question: I'm not sure is this is the right way to do this and if there is a more efficient way.

    Yes this is a good solution.

    Second question: How to sort. I would like to know how I could sort the table. Because of the {% for %} loop building the table, I don't know how to properly sort my table columns by clicking on the headers. I've seen many solutions but none would work with the loop in the table missing things up.

    You can either sort on the backend (render your template with sorted data from Django), or you can sort on the frontend (javascript). The loop shouldn't have anything to do with sorting on the frontend. Once your template is rendered, your html is just html, nothing special about it.