【发布时间】:2019-11-10 07:02:20
【问题描述】:
我正在尝试为每个问题返回投票最多的答案。我还想发送该答案的额外信息,例如 vote 和 id。
打印一个值很容易,但对于多个值,我必须返回字典。那么如何返回字典并打印模板中的所有值。
from django import template
register = template.Library()
@register.simple_tag
def getmostvotedanswer(answers):
answer = answers.order_by('-vote')[0]
answer_info = {
'answer':answer.answer,
'vote':answer.vote,
'id':answer.id
}
return answer_info
index.html
<p class="small text-muted ">{% getmostvotedanswer question.answer_set.all %}</p>
{'answer': 'THIS IS ANSWER THIS IS ANSWER THIS IS ANSWER THIS IS ANSWER THIS IS ANSWER', 'vote': 7, 'id': 1}
我可以为三个值调用 template_tag 3 次。
但我不想一次又一次地调用 templatetag 我认为它会影响性能。
view.py
def index(request):
questions = Question.objects.all()
context = {
'questions':questions
}
return render(request,'index.html',context=context)
编辑 -> 添加 view.py
【问题讨论】:
-
为什么要使用模板标签?在将其发送到模板之前,在视图上发送烹饪数据更容易且可调试。
-
@daniherrera 这不可能,因为我在模板中做反向关系。对于一个问题可以有多个答案,但在主页上我只想显示投票最高的答案。这就是我发送问题的原因使用 View 并使用 templatetag 获得最高票数的答案
-
什么是“最高票”? 3? 10 点?
-
@daniherrera 投票最多的先生
-
我的回答解决了你的问题吗?
标签: django templates templatetags