34👍
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
10👍
Well, it’s not :
{{ monthnumber|date:"M" }}
You should try this :
{{ datetimeobj|date:"M" }}
0👍
Just in case you do not want to format a date object passed into a Django template (like the other answers clearly show), but rather you’d prefer to simply get the current date’s month number;
here is the built-in tag you can use to do that:
{% now "n" %}
From the docs: https://docs.djangoproject.com/en/2.1/ref/templates/builtins/#now
Here are the built in date tags you can use with {% now [""] %}
https://docs.djangoproject.com/en/2.1/ref/templates/builtins/#date
- Django-autocomplete-light simple usage
- Filter on datetime closest to the given datetime
- How to use full_clean() for data validation before saving in Django 1.5 gracefully?
- Ruby HAML with Django?
0👍
The version of the custom filter with calendar
didn’t work for me (it takes string as a argument), so I created a different version of the filter with datetime.datetime.strptime
, that takes int as a argument:
import datetime
from django import template
register = template.Library()
@register.filter
def int_to_month_name(month_number):
datetime_object = datetime.datetime.strptime(month_number, "%m")
full_month_name = datetime_object.strftime("%B")
return full_month_name
Usage: {{ month_number|int_to_month_name}}
- Use imaplib and oauth for connection with Gmail
- Refresh <div> element generated by a django template
- Google App Engine Application Extremely slow
- Django forms give: Select a valid choice. That choice is not one of the available choices