django-对字段中的对象和值进行计数
我想在stats.html文件中包含一些关于模型的基本统计信息。这些变量不会显示在html中。我做错了什么django-对字段中的对象和值进行计数,django,count,sum,include,Django,Count,Sum,Include,我想在stats.html文件中包含一些关于模型的基本统计信息。这些变量不会显示在html中。我做错了什么 from django.shortcuts import render, get_object_or_404, redirect from django.db.models import Avg, Sum, Count from .models import Production def statistics(request): nr_of_plays = Production.
from django.shortcuts import render, get_object_or_404, redirect
from django.db.models import Avg, Sum, Count
from .models import Production
def statistics(request):
nr_of_plays = Production.objects.count()
nr_of_actors = Production.objects.aggregate(num_actors=Sum('nr_actors'))
nr_of_audience = Production.objects.aggregate(num_audience=Sum('est_audience'))
context = {
'nr_of_plays': nr_of_plays,
'nr_of_actors': nr_of_actors['num_actors'],
'nr_of_audience': nr_of_audience['num_audience'],
'test':'abc'
}
return render(request, 'stats.html', context)
模型:
class Production(models.Model):
title = models.CharField(max_length=200)
nr_actors = models.IntegerField(default=0)
est_audience = models.IntegerField(default=0)
...
URL.py:
path('stats/', views.statistics, name='stats'),
base.html的相关部分:
<copyright class="text-muted">
<div class="container text-center">
<p>© One World Theatre - {% now "Y" %} {% include 'stats.html' with test=test %} </p>
</div>
</copyright>
输出:
编辑:
我没有提到我在base.html
模板中使用的模板是{%include'stats.html%}
。当我将带有test=test的添加到include标记时,将显示测试文本。但是当使用nr\u of_plays=nr\u of_plays添加时,不会发生任何事情:-/
我最终忘记了在我的基本模板中尝试{%include'stats.html%}
,只是在需要的地方添加了这些变量,效果很好。不干,但该怎么办
编辑2:
我太快了,哭不出胜利来。用最新代码编辑了问题。在处理主内容块的视图中传递变量是可行的,但这意味着我必须在每个视图中添加它们(不是干式)。仍然无法获得与我的设置不兼容的内容example.com/stats.html
呈现的正是我想要的内容,但当我将它包含在我的base.html
中时,不会显示变量<代码>with test=test
不起任何作用。不知所措(感谢迄今为止的帮助)。返回一本词典
您需要通过键访问其值
context = {
'nr_of_plays': nr_of_plays,
'nr_of_actors': nr_of_actors['nr_actors_sum'],
'nr_of_audience': nr_of_audience['est_audience_sum']
}
或者,您可以指定自定义键名,而不是默认的复合键名:
nr_of_actors = Production.objects.aggregate(num_actors=Sum('nr_actors'))
nr_of_audience = Production.objects.aggregate(num_audience=Sum('est_audience'))
注意:.all()
是多余的,可以删除返回字典
您需要通过键访问其值
context = {
'nr_of_plays': nr_of_plays,
'nr_of_actors': nr_of_actors['nr_actors_sum'],
'nr_of_audience': nr_of_audience['est_audience_sum']
}
或者,您可以指定自定义键名,而不是默认的复合键名:
nr_of_actors = Production.objects.aggregate(num_actors=Sum('nr_actors'))
nr_of_audience = Production.objects.aggregate(num_audience=Sum('est_audience'))
注意:.all()
是多余的,可以删除根据您最新的供述和症状,您似乎不想查看您的统计数据
看起来url正在呈现另一个视图,该视图也扩展了base.html
,这让您感到困惑,认为您处于正确的视图中
测试它的一种方法是在statistics
视图中放置一个print语句,查看它是否在控制台中打印任何内容:
def统计信息(请求):
打印(111111111111111111111)
...
返回呈现(请求'stats.html',上下文)
第二件事是,如果base.html
包含stats.html
,则不应直接呈现stats.html
,应将上下文传递给扩展base.html
的模板
第三件事是,请参考Pynchia的答案,以正确获取聚合查询集的计数。根据您最近的供述和症状,您似乎不会进入您的统计视图
看起来url正在呈现另一个视图,该视图也扩展了base.html
,这让您感到困惑,认为您处于正确的视图中
测试它的一种方法是在statistics
视图中放置一个print语句,查看它是否在控制台中打印任何内容:
def统计信息(请求):
打印(111111111111111111111)
...
返回呈现(请求'stats.html',上下文)
第二件事是,如果base.html
包含stats.html
,则不应直接呈现stats.html
,应将上下文传递给扩展base.html
的模板
第三件事是,请参考Pynchia的答案,以正确获取聚合查询集的计数。哪些变量未显示?所有这些?事实上,没有一个显示。变量外的模板文本显示了吗?是的,文本显示如下:“-Stats:plays producted,included actors,seen by people.”并且我确保在生产对象中有数字可以使用。自己做一个测试,将额外的元素传递到上下文中,如{test':'abc'}
,然后将其添加到模板中,查看是否显示。哪些变量不显示?所有这些?事实上,没有一个显示。变量外的模板文本显示了吗?是的,文本显示如下:“-Stats:plays producted,included actors,seen by people.”并且我确保在生产对象中有数字可以使用。自己做一个测试,将额外的元素传递到上下文中,如{test':'abc'}
,然后把它添加到你的模板中,看看它是否出现。我本想建议,但他说没有一个变量出现,我本以为至少应该显示nr\u个播放
。现在发生了什么?我猜对这些键的错误访问可能阻止了整个渲染。嗯,甚至将Production.objects.all().aggregate(Sum('est_audition'))
传递到上下文,它仍然会显示queryset repr,但他说没有显示任何内容,但这也令人困惑,他是说即使是模板文本也没有出现吗?我本来想建议,但他说没有一个VAR出现,我本以为至少应该显示nr\u个播放
。现在发生了什么?我猜对这些键的错误访问可能会阻止整个渲染。嗯,甚至将Production.objects.all().aggregate(Sum('est_-academy'))
传递到上下文,它仍然会显示queryset报告,但他说什么都没有显示,但这也令人困惑,他是说即使模板文本也没有显示吗?