Django:在一个页面上显示多个对象

时间:2017-09-25 17:12:28

标签: python django

我在index.html

显示的幻想篮球联赛中有lot of data

当我点击一个玩家名字时,我被带到该玩家的single season's statistics

当我点击一个玩家名字时,我想要发生的是all of that player's career statistics to show up,而不仅仅是一年。

这样做的最佳方法是什么?我已经坚持了两个星期了,哈哈,

注意:每个'玩家'对象都包含一个独立的“game_id”,属于每个真实玩家(例如Aaron Brooks objects列表中的每个玩家对象都有相同的“game_id”),但我没有'能够弄清楚如何使用它!

提前致谢!

Views.py:

class IndexView(generic.ListView):
    template_name = 'polls/index.html'
    context_object_name = 'players_list'

    def get_queryset(self):
        return Player.objects.order_by('full_name', 'season', 'team', '-ppg', 'full_name')


class PlayerView(generic.DetailView):
    model = Player
    template_name = 'polls/player.html'

Models.py:

class Player(models.Model):
    def __str__(self):
        return self.full_name

    first_name = models.CharField(max_length=200, default='')
    surname = models.CharField(max_length=200, default='')
    full_name = models.CharField(max_length=200, default='')
    nba_live_id = models.IntegerField(default=0)

    season = models.CharField(max_length=4, default='')
    team = models.CharField(max_length=3, default='') 
    pos = models.CharField(max_length=2, default='')    

    gp = models.IntegerField(default=0)
    gs = models.IntegerField(default=0)
    ppg = models.FloatField(default=0)
    rpg = models.FloatField(default=0)
    apg = models.FloatField(default=0)
    spg = models.FloatField(default=0)
    bpg = models.FloatField(default=0)

的index.html:

{% load static %}

<link rel="stylesheet" type="text/css" href="{% static 'polls/style.css' %}" />

{% if players_list %}
    <table>
        <tr>
            <td>Season</td>
            <td>Team</td>
            <td>GP</td>
            <td>GS</td>
            <td>Pos</td>
            <td>Player Name</td>
            <td>PPG</td>
            <td>RPG</td>
            <td>APG</td>
            <td>BPG</td>
            <td>SPG</td>

            <td></td>
        </tr>

    {% for player in players_list %}
        <tr>
            <td>{{ player.season }}</td>
            <td>{{ player.team }}</td>
            <td>{{ player.gp }}</td>
            <td>{{ player.gs }}</td>
            <td>{{ player.pos }}</td>
            <td><a href="{% url 'polls:player' player.id %}">{{ player.full_name }}</a></td>
            <td>{{ player.ppg }}</td>
            <td>{{ player.rpg }}</td>
            <td>{{ player.apg }}</td>
            <td>{{ player.bpg }}</td>
            <td>{{ player.spg }}</td>
        </tr>
    {% endfor %}
    </table>
{% endif %}

Player.html:

<h1>{{ player.full_name }}</h1>
<h1>{{ question.question_text }}</h1>


    <table>
        <tr>
            <td>Season</td>
            <td>Team</td>
            <td>GP</td>
            <td>GS</td>
            <td>Pos</td>
            <td>Player Name</td>
            <td>PPG</td>
            <td>RPG</td>
            <td>APG</td>
            <td>BPG</td>
            <td>SPG</td>

            <td></td>
        </tr>

        <tr>
            <td>{{ player.season }}</td>
            <td>{{ player.team }}</td>
            <td>{{ player.gp }}</td>
            <td>{{ player.gs }}</td>
            <td>{{ player.pos }}</td>
            <td>{{ player.full_name }}</td>
            <td>{{ player.ppg }}</td>
            <td>{{ player.rpg }}</td>
            <td>{{ player.apg }}</td>
            <td>{{ player.bpg }}</td>
            <td>{{ player.spg }}</td>
        </tr>
    </table>

1 个答案:

答案 0 :(得分:0)

您的模型结构错误。您需要将季节信息分离为新模型,因此您可以通过ForeignKey链接Player和PlayerSeason。 (您可能也想将团队分开,甚至可能将Season本身分开,但这不属于这个问题的范围。)

所以,PlayerSeason可能看起来像这样:

class PlayerSeason(models.Model):
    player = models.ForiegnKey('Player')
    team = models.CharField(max_length=3, default='') 
    pos = models.CharField(max_length=2, default='')    

    gp = models.IntegerField(default=0)
    gs = models.IntegerField(default=0)
    ppg = models.FloatField(default=0)
    rpg = models.FloatField(default=0)
    apg = models.FloatField(default=0)
    spg = models.FloatField(default=0)
    bpg = models.FloatField(default=0)

现在,您可以在模板中迭代所有玩家的个人原因:

<h1>{{ player.full_name }}</h1>
<h1>{{ question.question_text }}</h1>


    <table>
    ...
    {% for season in player.playerseason_set.all %}
       <tr>
         <td>{{ player.gp }}</td>
         ...
       </tr>
    {% endfor %}

您希望设置管理网站,以便PlayerSeason也可以edited inline with Player