将月份编号转换为月份名称

1 python django

下面的代码正确获取月份编号,但我想检索月份名称而不是数字。我试过在模板中使用 Django 日期过滤器,以及在 views.py 中使用日历工具,但这似乎不起作用

视图.py

def ticket(request, month, year):
    airline = Airline.objects.get(id=request.user.airline_id)
    for ts in Timestamp.objects.filter(
            airline=airline,
            usage_on__year=year,
            usage_on__month=month
    ):
        pass

    return TemplateResponse(request, 'club/al_history.html', {
        'usage_month': month,
        'usage_year': year,
        'timestamp': timestamp,
    })
Run Code Online (Sandbox Code Playgroud)

al.html

{% extends 'base.html' %}

{% block content %}
<h3>{{ usage_month }}, {{usage_year}}</h3>
{% endblock %}
Run Code Online (Sandbox Code Playgroud)

Ron*_*eau 5

您可以使用calendar.month_name. 根据其文档,它是:

一个数组,表示当前语言环境中一年中的几个月。

所以你可以像这样简单地使用它:

calendar.month_name[month]
Run Code Online (Sandbox Code Playgroud)

完整示例,使用en_US语言环境:

>>> import calendar
>>> month = 1
>>> calendar.month_name[month]
'January'
Run Code Online (Sandbox Code Playgroud)