给我两个模型,Deck和Flashcard:
class Deck(models.Model):
owner = models.ForeignKey(User, on_delete=models.CASCADE)
name = models.CharField(max_length=255)
class Flashcard(models.Model):
owner = models.ForeignKey(User, on_delete=models.CASCADE)
deck = models.ForeignKey(Deck, on_delete=models.CASCADE)
question = models.TextField()
answer = models.TextField()
我想显示特定牌组的详细信息,例如该卡片组的问题和答案列表。
因此,在我的Deck模型中,我具有以下功能:
def list_flashcards(self):
fc_list = Flashcard.objects.filter(deck=self).values_list('question', flat=True)
return fc_list
现在在我的html模板中,如果我使用:
{{deck.list_flashcards}}
我得到:<QuerySet ['first', 'second','third']
换句话说,我得到了正确的项目,只是格式不正确。我怎样才能将其作为“正常”列表?
例如,当我使用...
{{deck.list_flashcards.0}} <br>
{{deck.list_flashcards.1}} <br>
...有效。但是我不知道用户将拥有多少张卡,这当然也不有效。
我想做的是这样的:
{% for fc in fc_list %}
Question: {{fc}}
{% endfor %}
但是它不起作用-网站上没有任何内容。
我应该将此添加到我的视图中以使其起作用吗?
你太亲密了!
{% for card in deck.list_flashcards %}
Question: {{card}}
{% endfor %}