Template filter {{ value|date:"M" }} in Python

15 views
Skip to first unread message

Carsten Fuchs

unread,
Nov 6, 2018, 9:43:19 AM11/6/18
to Django users
Hello,

complementing template filters, sometimes it is necessary to format dates also
in Python code, that is, achieve the equivalent to

{{ value|date:"M" }}

For this, I'm using

from django.utils import dateformat
dateformat.format(value, "M")

(This works in Django views and in test cases, but not in `./manage.py shell`.)

As `dateformat.format()` is not covered in
https://docs.djangoproject.com/en/2.1/ref/utils/, but still very useful e.g. in
tests, my question is if this is the correct way to achieve in Python what the
`date` template filter does? Are there alternatives? (`strftime()` does not
provide translated months names etc., and so isn't).

Best regards,
Carsten
Reply all
Reply to author
Forward
0 new messages