Converting month number to month name
Question:
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 %}
Answers:
You can use the calendar module:
calendar.month_name[3]
returns March
For more information check out this question
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])
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'
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.
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".
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 %}
You can use the calendar module:
calendar.month_name[3]
returns March
For more information check out this question
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])
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'
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.
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".