How to display month name by number?

后端 未结 4 569
囚心锁ツ
囚心锁ツ 2021-02-07 11:24

I have a number represented month and i want to replace it with month name, date filter not working:

{{ monthnumber|date:\"M\" }}

I want to pla

4条回答
  •  误落风尘
    2021-02-07 12:01

    You'll need a custom template filter to convert the number into a month name. Pretty simple if you use the calendar module from the standard library:

    import calendar
    
    @register.filter
    def month_name(month_number):
        return calendar.month_name[month_number]
    

    Usage example: {{ 5|month_name }} will output May

提交回复
热议问题