LukeWalker
LukeWalker

Reputation: 7

Django populate html table with list values

I am trying to populate my html table with data from parsed txt file using Django. For now my parse.py program serves txt file in list, example:

[['1523501752', 'mac', '192.168.1.180', 'Device1', 'mac'], 
['1523514991', 'mac', '192.168.1.113', 'device2', 'mac']]

Every inside list represents one line txt file (information about device). I am saving this list in variable and using it as a context. Problem is that I need specific arguments from lists. I mean in html table i will have three columns and as much rows as there will be lists in context. Those three columns will be ip address, device name and mac address.

For now my code looks like this:

    <tbody>
        {% for line in lines  %}
        <tr>
          {% for value in line%}
            <td>{{ value.1}}</td>
            <td>{{ value.2 }}</td>
            <td>{{ value.3 }}</td>
        </tr>
          {% empty %}
          <tr>
              <td colspan="8" class="text-center bg-warning"> Device not found </td>
          </tr>
          {% endfor %}
        {% endfor %}
    </tbody>

But results are really not as expected... DTL takes just 1 symbol at a time.. How could I solve this problem?

Tried to search information in:

Django : HTML Table with iterative lists values and How to populate html table with info from list in django

Unfortunately there was no right answer.

Upvotes: 0

Views: 2569

Answers (1)

Grant Anderson
Grant Anderson

Reputation: 362

It's a little hard to say for sure without seeing the rest of your code, but it looks like you only need to iterate through the initial list of lists.

Then you can refer to the specific values you want in each list via their index.

For example:

<tbody>
    {% for line in lines  %}
    <tr> 
        <td>{{ line.0 }}</td>
        <td>{{ line.1 }}</td>
        <td>{{ line.2 }}</td>
    </tr>
    {% endfor %}
</tbody>

Upvotes: 1

Related Questions