Reputation: 523
I have a number represented month and I want to replace it with month name. The date filter not working, as it works with datetime, and I have the field as integer. For sure the value will be between 1 and 12.
{{ monthnumber|date:"M" }}
Please help.
Upvotes: 0
Views: 1144
Reputation: 523
Thanks all for the valuable solutions. Instead of using custom filter or any 3rd party code.. I tend to add an additional field for Month Name along with Month Number in the queryset itself.. and in HTML template i used that additional field for Month Name for display purpose. This seems to be fast.
Upvotes: 0
Reputation: 92
From what I read I assume you are just passing an integer. You have to pass a value that is a datetime object, ie in your views context, monthname must be a datetime object.
If you would still like to work with an integer from 1 to 12, you could write your own filter with something along the lines:
@register.filter
def monthtextual(value):
return datetime.date(2020, value, 1).strftime('%B')
See https://docs.djangoproject.com/en/3.0/howto/custom-template-tags/ for more details.
Upvotes: 1