为什么团队名称不显示在Django HTML中?

时间:2018-07-05 08:04:31

标签: python django django-models

migrations.CreateModel(
    name='BasketballScore',
    fields=[
        ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
        ('match_played', models.IntegerField(default='0')),
        ('lose', models.IntegerField(default='0')),
        ('win', models.IntegerField(default='0')),
        ('points', models.IntegerField(default='0')),
        ('team', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='teams_basketball', to='Schedule.Team')),
    ],
),

这是我的迁移文件夹中的一部分,我认为这确认了我的 BasketballScore 模型是否正确。

class BasketballScore(models.Model):
    team = models.ForeignKey(Team, related_name='teams_basketball', on_delete=models.CASCADE)
    match_played = models.IntegerField(default='0')
    lose = models.IntegerField(default='0')
    win = models.IntegerField(default='0')
    points = models.IntegerField(default='0')

这是 BasketballScore 的实际模型。

class BasketballScoreView(generic.ListView):
    context_object_name = 'matches'
    template_name = 'Schedule/basketball_score.html'
    queryset = BasketballScore.objects.all()

    def get_queryset(self):
        return BasketballScore.objects.all().order_by('-points')

这是views.py中的相应类 BasketballScoreView

{% extends 'Schedule/base.html' %}
{% block content %}

<div class="container">
<p><button onclick="window.location='{% url 'index' %}'" class="btn btn-default">Go Back</button></p>

<div class="table-responsive">
<table class="table table-bordered table-hover">
  <tr>
  <th>Teams</th>
  <th>Match</th>
  <th>Win</th>
  <th>Lose</th>
  <th>Points</th>
  </tr>
{% for match in matches %}


<tr>
  <td>
  <div style="width='100';height='20'"><img src="{{ match.team.image.url }}"></div>
  {{match.team}}
    </td>

  <td><div class="text-center">{{match.match_played}}</div></td>
  <td><div class="text-center">{{match.win}}</div></td>
  <td><div class="text-center">{{match.lose}}</div></td>
  <td><div class="text-center">{{match.points}}</div></td>

</tr>

{% endfor %}
</table>
</div>
</div>

{% endblock %}

现在这是 basketball_score.html 页面。

<!DOCTYPE html>
<html lang="en">
<head>
    <style>
        .horizontal { display: inline; background-color: lightgray; }
    </style>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Schedule</title>
  {% load static %}
  <!-- <link rel="stylesheet" href="{% static 'Schedule/css/bootstrap.css' %}" />
  <link rel="stylesheet" href="{% static 'Schedule/css/style.css' %}" /> -->

  {% block title %}

  {% endblock %}

  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>

  <!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">

<!-- Optional theme -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css" integrity="sha384-rHyoN1iRsVXV4nD0JutlnGaslCJuC7uwjduW9SVrLvRYooPp2bWYgmgJQIXwl/Sp" crossorigin="anonymous">

<!-- Latest compiled and minified JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>


</head>
<body>

  {% block content %}

  {% endblock %}
</body>
<!-- <script src="{% static 'Schedule/js/calendar.js' %}"></script> -->
<!-- <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> -->
<script>
  var csrf_token = '{{ csrf_token }}'
</script>
</html>

现在上面的代码是 base.html ,我从中扩展了 basketball_score.html 。因此,当我单击“得分表” 按钮时,该按钮将我带到Basketball_score.html页,除了表头是第 部分之外,什么都没有显示,没有团队名称等。现在我添加相应的urls.py:

path('match/basketball_score/', views.BasketballScoreView.as_view(), name='basketball_score'),

任何帮助将不胜感激! :)

1 个答案:

答案 0 :(得分:0)

确保数据库中实际上有任何BasketballScore对象。