D
D
Dmitry2018-11-02 19:02:17
Django
Dmitry, 2018-11-02 19:02:17

How to make a table output and with a variable number of columns?

Specialists programmers tell me how to do what is in the title of the question, an example of a table.
No. Name 1 2 3+n ... Rating Notes
-------------------------------------- ---------------
1 you yu in in ... 33 dlvoadylva

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vladislav Klimanov, 2018-11-05
@topwebmaster

from collections import namedtuple
from operator import attrgetter
from django.views.generic import TemplateView

PLAYERS_COUNT = 10
Fight = namedtuple('Fight', ['player_no', 'opponent', 'points'])


class Player:
    name = ''
    fights = None
    result = 0.

    def __init__(self, name='', fights=None):
        self.name = name
        self.fights = fights or []
        self.result = sum(filter(None, self.fights))


class RankingTableView(TemplateView):
    template_name = 'ranking_table.htm'

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)

        fights = [
            Fight(1, 4, 1.),
            Fight(1, 8, 1.),
            Fight(1, 2, 1.),
            Fight(1, 6, 1.),
            Fight(1, 3, 1.),

            Fight(2, 7, 1.),
            Fight(2, 3, 1.),
            Fight(2, 1, 0.),
            Fight(2, 9, 1.),
            Fight(2, 5, 1.),

            Fight(3, 6, 1.),
            Fight(3, 2, 0.),
            Fight(3, 9, 1.),
            Fight(3, 5, 1.),
            Fight(3, 1, 0.),

            Fight(4, 1, 0.),
            Fight(4, 5, 0.),
            Fight(4, 7, 1.),
            Fight(4, 8, 1.),
            Fight(4, 6, 1.),
        ]

        fight_data = [
            [None]*PLAYERS_COUNT for _ in range(PLAYERS_COUNT)
        ]

        for fight in fights:
            player_index = fight.player_no - 1
            opponent_index = fight.opponent - 1
            fight_data[player_index][opponent_index] = fight.points

        players = [Player(f'Боец №{i+1}', fights) for i, fights in enumerate(fight_data)]
        players = sorted(players, key=attrgetter('result'), reverse=True)

        table = 

        for i, player in enumerate(players):
            row = [i + 1, player.name] + player.fights + [player.result, i + 1]
            table.append(row)

        context['table'] = table
        return context

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Ranking Table</title>
    <style>
        table {
            border-collapse: collapse;
        }
        table, th, td {
            border: 1px solid black;
        }
    </style>
</head>
<body>

<h1>Ranking Table</h1>

<table>
{% for row in table %}
    <tr>
    {% for column in row %}
        <td>{{ column | default_if_none:'' }}</td>
    {% endfor %}
    </tr>
{% endfor %}
</table>

</body>
</html>

5be0769d6899e634641910.png

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question