44๐
use the datetime string formatting method, e.g.
>>> today.strftime('%B')
'March'
for more info, and a full list of formatting codes, see the python datetime
docs
34๐
For English only, you can use the datetime string formatting method of Python, e.g.
>>> today.strftime('%B')
'March'
You can also use Django methods, which will return the name in the current activated language.
In a Django template you can also use:
{{ a_date|date:'F' }}
In a Django view function:
from django.template.defaultfilters import date
date(a_date, 'F')
You can test the later in the Django shell (python manage.py shell
) for e.g. Spanish with:
In [1]: from django.utils.translation import get_language, activate
In [2]: activate('es')
In [3]: get_language()
Out[3]: 'es'
In [4]: import datetime
In [5]: today = datetime.date.today()
In [6]: from django.template.defaultfilters import date
In [7]: date(today, 'F')
Out[7]: u'Septiembre'
- [Django]-Celery Flower Security in Production
- [Django]-Django query negation
- [Django]-Django internationalization for admin pages โ translate model name and attributes
10๐
The Calendar API is another option.
calendar.month_name[3] # would return 'March'
- [Django]-How to make Django restart runserver on template change?
- [Django]-How can I programmatically obtain the max_length of a Django model field?
- [Django]-Django apps aren't loaded yet when using asgi
3๐
I solved the same by registering a custom template tag to help me apply it on any template page that requires the month name.
templatetags/etras.py
from django import template
import calendar
register = template.Library()
@register.filter
def month_name(value):
return calendar.month_name[value]
Usage in template:
{% load extras %}
{{ month_figure|month_name }}
- [Django]-Connection reset by peer when using s3, boto, django-storage for static files
- [Django]-ValueError: Cannot add *: instance is on database "default", value is on database "None"
- [Django]-Why does Django REST Framework provide different Authentication mechanisms
2๐
datetime.datetime.strftime(today, '%B') # Outputs 'March'
or
datetime.datetime.strftime(today, '%b') # Outputs 'Mar'
http://docs.python.org/library/datetime.html#strftime-strptime-behavior
- [Django]-Images from ImageField in Django don't load in template
- [Django]-How to limit file types on file uploads for ModelForms with FileFields?
- [Django]-How can I use the variables from "views.py" in JavasScript, "<script></script>" in a Django template?
2๐
Month Name
to Month Number
and vice versa in python
By Using calendar calendar
Creating a reverse dictionary
would be a reasonable way to do this:
dict((val,k) for k,v in enumerate(calendar.month_abbr))
- [Django]-How to translate docker-compose.yml to Dockerrun.aws.json for Django
- [Django]-__init__() got an unexpected keyword argument 'user'
- [Django]-Version number in Django applications
1๐
Pythonโs calendar module will do that for you, see calendar.month_name
month = calendar.month_name[3]
- [Django]-Deploying a local django app using openshift
- [Django]-Django โ How to sort queryset by number of character in a field
- [Django]-Is there a way to loop over two lists simultaneously in django?
0๐
Use strftime:
>>> today = datetime.datetime.now()
>>> today.strftime("%B")
'March'
>>>
- [Django]-No URL to redirect to. Either provide a url or define a get_absolute_url method on the Model
- [Django]-How to get a favicon to show up in my django app?
- [Django]-How to change field name in Django REST Framework