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

2024-09-28 23:27:58 发布

您现在位置:Python中文网/ 问答频道 /正文

下面的代码正确地获取了月份编号,但我想检索月份名称,而不是数字。我尝试过在模板中使用Django日期过滤器,以及中的日历实用程序视图.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,
    })

铝.html

^{pr2}$

Tags: 代码py视图idobjectsonrequesthtml
3条回答

您可以使用日历模块:

calendar.month_name[3]

返回March

有关详细信息,请查看this question

您可以使用calendar.month_name根据its documentation,它是:

An array that represents the months of the year in the current locale.

所以你只需这样使用它:

calendar.month_name[month]

完整示例,使用en_US区域设置:

^{pr2}$

使用key=month number和value=month name创建一个字典,如下所示:

dict = {"01": "January", "02": "February", etc...}

然后你可以这样用这个词:

^{pr2}$

相关问题 更多 >