在 django 视图中,我过滤掉每年的内容,以通过模板将其显示在图表中。目前我正在为每年的手册进行计算,但必须有更好的方法来纠正代码,以便它只按年份过滤内容,而不必对年份进行硬编码。同样在图表模板中,我也在对年份进行硬编码,并且想知道是否可以在没有硬编码的情况下编写。
content_count_2006 = ContentItem.objects.filter(timestamp__year=2006).count()
content_count_2007 = ContentItem.objects.filter(timestamp__year=2007).count()
content_count_2008 = ContentItem.objects.filter(timestamp__year=2008).count()
content_count_2009 = ContentItem.objects.filter(timestamp__year=2009).count()
content_count_2010 = ContentItem.objects.filter(timestamp__year=2010).count()
content_count_2011 = ContentItem.objects.filter(timestamp__year=2011).count()
content_per_year = [content_count_2006, content_count_2007, content_count_2008, content_count_2009, content_count_2010, content_count_2011]
{% chart VerticalBarStack content_per_year %}
{% axes type xy %}
{% axes label "2006" "2007" "2008" "2009" "2010" "2011" %}
{% color CCCCCC %}
{% endchart %}