1

The code below fetches the month number correctly, but I want to retrieve the month name and not the number. I've tried using Django date filters in the template, as well as Calendar utils in views.py but that doesn't seem to work

views.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,
    })

al.html

{% extends 'base.html' %}

{% block content %}
<h3>{{ usage_month }}, {{usage_year}}</h3>
{% endblock %}
Ronan Boiteau
  • 9,608
  • 6
  • 34
  • 56
  • 2
    Possible duplicate of [Get month name from number](https://stackoverflow.com/questions/6557553/get-month-name-from-number) – Gareth O'Connor Mar 23 '18 at 15:05
  • I solved it using the date utils in views.py, as: 'usage_date': date(int(year), int(month), 1) And then passing this to my template, rendering the month names using Django shortcuts. This works well if I want the month name to change in accordance with the language chosen. –  Mar 23 '18 at 15:49
  • @Angy You should post this as an answer. – Ronan Boiteau Mar 23 '18 at 16:03

5 Answers5

6

You can use calendar.month_name. According to its documentation, it is:

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

So you would simply use it like this:

calendar.month_name[month]

Full example, with en_US locale:

>>> import calendar
>>> month = 1
>>> calendar.month_name[month]
'January'
Ronan Boiteau
  • 9,608
  • 6
  • 34
  • 56
  • Thank you, Ronan! It works very well. But it was confined to rendering the month names only in English, regardless of the language preference. So, I opted for a different way to solve it –  Mar 23 '18 at 15:51
3

You can use the calendar module:

calendar.month_name[3]

returns March

For more information check out this question

Ronan Boiteau
  • 9,608
  • 6
  • 34
  • 56
Nathan
  • 3,558
  • 1
  • 18
  • 38
1

Create a dictionary with key = month number and value = month name like:

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

then you can use this dict like so:

print(my_dict[month])
Nathan
  • 3,558
  • 1
  • 18
  • 38
Kyle Joeckel
  • 469
  • 2
  • 9
  • 1
    The answer is alright but it is not a good practice to use built-in names like `dict` for custom variables. – xyres Mar 23 '18 at 15:34
0

I solved it using the date utils in views.py, as:

'usage_date': date(int(year), int(month), 1) 

And then in the template, rendered the month names using Django shortcuts.

This works well if I want the month name to change in accordance with the language chosen.

0

From the answer here:

import datetime

monthinteger = 12

month = datetime.date(1900, monthinteger, 1).strftime('%B')

print(month)

would give "December". Also though, replacing '%B' with '%b' will give the abbreviated form e.g. "Dec".

Relative0
  • 1,567
  • 4
  • 17
  • 23