如何显示模板中的模型字段

时间:2020-08-14 18:41:46

标签: django-models

我正在尝试从模型中提取数据并将其显示给模板,但是未显示数据。任何帮助将不胜感激。 这是模型:

class Schedule(models.Model):

    team1 = models.CharField(max_length=50)
    team1_pic = models.ImageField()
    team2 = models.CharField(max_length=50)
    team2_pic = models.ImageField()
    timestamp = models.TimeField(default="", blank=True, null=True)
    datestamp = models.DateField(default="", blank=True, null=True)
    location = models.CharField(max_length=200)

这是视图:

def schedule(request):
    schedule = Schedule.objects.all()
    context = {
        'schedule': schedule
    }
    return render(request, 'schedule.html')

最后是模板:

   <!-- Schedule Section Begin -->
<section class="schedule-section spad">
    <div class="container">
        <div class="row">
            <div class="col-lg-8 left-blog-pad">
                <div class="schedule-text">
                    <h4 class="st-title">World Cup 2019</h4>
                    <div class="st-table">
                        {% for sch in schedule %}
                        <table>
                            <tbody>
                                <tr>
                                    <td class="left-team">
                                        <img src="{% static 'img/schedule/flag-1.jpg' %}" alt="">
                                        <h4>{{ sch.team1 }}</h4>
                                    </td>
                                    <td class="st-option">
                                        <div class="so-text">{{ sch.location }}</div>
                                        <h4>VS</h4>
                                        <div class="so-text">{{ sch.date }}</div>
                                    </td>
                                    <td class="right-team">
                                        <img src="{% static 'img/schedule/flag-2.jpg' %}" alt="">
                                        <h4>{{ sch.team1 }}</h4>
                                    </td>
                                </tr>
                            </tbody>
                        </table>
                    </div>
                    {% endfor %}
                </div>
            </div>

1 个答案:

答案 0 :(得分:0)

您还需要传递上下文。

def schedule(request):
        schedule = Schedule.objects.all()
        context = {
            'schedule': schedule
        }
        return render(request, 'schedule.html',context)