【问题标题】:Django template tag formatDjango 模板标签格式
【发布时间】:2017-08-31 16:52:28
【问题描述】:

我在模型上有以下方法:

@property
def get_total_planned_stops(self, *args, **kwargs):
    owner = self.owner
    planned_total = DailyRoute.objects.filter(stage=1).aggregate(Sum('planned_stops'))
    return planned_total

在我的模板中,变量{{ item.get_total_planned_stops}} 呈现为:

{'planned_stops__sum': 183}

我只想显示数字。我做错了什么?

【问题讨论】:

    标签: django django-models django-templates


    【解决方案1】:

    aggregate 函数返回一个 dict 格式的数据,这就是您在输出中看到 dict 的原因。

    尝试访问键值。

     {{ item.get_total_planned_stops.planned_stops__sum }}
    

    【讨论】:

      猜你喜欢
      • 2019-10-09
      • 2021-02-18
      • 1970-01-01
      • 2011-02-09
      • 2012-10-31
      • 1970-01-01
      • 2014-03-20
      • 1970-01-01
      • 2014-07-01
      相关资源
      最近更新 更多