所以我現在剛剛與Django握手,Django模板邏輯
我想要一些相對悲觀的事情。我有這樣一個模型:在我看來
class Article(models.Model):
id = models.IntegerField(primary_key=True)
volnumber = models.IntegerField(db_column='volNumber')
title = models.TextField()
keywords = models.TextField(blank=True)
start_page = models.IntegerField(null=True, blank=True)
end_page = models.IntegerField(null=True, blank=True)
author_1 = models.TextField(blank=True)
author_2 = models.TextField(blank=True)
author_3 = models.TextField(blank=True)
author_4 = models.TextField(blank=True)
author_5 = models.TextField(blank=True)
author_6 = models.TextField(blank=True)
author_7 = models.TextField(blank=True)
author_8 = models.TextField(blank=True)
author_9 = models.TextField(blank=True)
author_10 = models.TextField(blank=True)
:
def index(request):
article_list = Article.objects.all()
volume_list = Volume.objects.all()
auth_list = ['author_1', 'author_2', 'author_3', 'author_4', 'author_5', 'author_6', 'author_7', 'author_8', 'author_9', 'author_10', ]
return render_to_response('index.html', {
'article_list': article_list,
'volume_list': volume_list,
'auth_list' : auth_list,
})
,我想遍歷第一篇的作者,以及消除對模板列表爲空項:
<ul class="articleList">
{% for article in article_list %}
<li>
{% for author in auth_list %}
<i>{{ article."author" }}</i>
{% endfor %}
{{ article.title }}{{ article }}
</li>
{% endfor %}
</ul>
很明顯,這不起作用,我不確定模板是否適用於此邏輯,但我希望從中可以明白我正在努力實現的目標。
任何幫助非常感謝。
您確定您的文章有足夠的作者字段?您可能需要更多! – SingleNegationElimination