所以我要上这堂课。现在对于这节课,我有两个选择:“课堂”和“研讨会”,我希望每次我在其中一节课上加一节课,模板会先显示选择,然后显示所有的课。 例如:课程将有讲座1、讲座2、讲座3等。 现在的问题是,当我迭代时,每次都会显示choice,每次讲座都会显示choice,我希望每个choice只显示一次
class Lecture(models.Model):
course = models.ForeignKey('Course', on_delete=models.CASCADE, default='', related_name='lectures')
lecture_category = models.IntegerField(choices=((0, "Classes "),
(1, "Seminars"),
))
lecture_title = models.CharField(max_length=100)
content = models.TextField()
link = models.URLField(blank=True)
file = models.FileField(upload_to='documents', blank=True)
def __str__(self):
return self.lecture_title
<ul>
{% for c in lectures %}
<b>{{ c.get_lecture_category_display }}</b>
<p>.......................</p>
<li>{{ c.lecture_title }}</li>
<li>{{ c.content }}</li>
{% if c.link %}
<li>{{ c.link }}</li>
{% if c.file %}
<li><a href='{{ MEDIA_URL }}{{ c.file.url }}'>download</a></li>
{% endif %}
{% endif %}
{% endfor %}
<hr/>
</ul>
有一个名为regroup的模板标记可用于此操作。参见https://docs.djangoproject.com/en/2.0/ref/templates/builtins/的重组部分
编辑: 正如danielroseman所指出的,您还必须按照您希望在视图中重新组合的字段对查询进行排序。在这种情况下,你必须按讲座类别来安排讲座。否则上述方法将不起作用
相关问题 更多 >
编程相关推荐