【发布时间】:2020-03-03 09:09:05
【问题描述】:
我是 Django 框架的新手,我使用注释来总结一年中特定月份支付的所有金额。 这工作正常,但问题是无法将月份 int 转换为模板中的月份名称
views.py
def income_report(request):
context = {}
if grab_data_passed == 'monthly':
daily_data=Income.objects.values('date__year', 'date__month')\
.annotate(amount=Sum('amount'))
context['daily_data']=daily_data
return render(request, 'reports/incomes_report_details.html', context)
模板
{% for fo in daily_data %}
<tr>
<td>{{ fo.month }}</td>
<td>{{ fo.amount }}</td>
</tr>
{% endfor %}
如何在模板中将此带注释的月份更改为月份名称。 我知道我必须先将 date__month 转换回视图中的月份名称,然后才能将其传递给模板,但是如何?
【问题讨论】:
-
您能添加您的
Income模型吗? -
检查这个答案,它的答案比接受的答案更好stackoverflow.com/a/7385976
-
这能回答你的问题吗? How to display month name by number?