[Django] #26281: utils.formats.date_format() does not work with datetime.date

31 views
Skip to first unread message

Django

unread,
Feb 25, 2016, 10:51:06 AM2/25/16
to django-...@googlegroups.com
#26281: utils.formats.date_format() does not work with datetime.date
-------------------------------+--------------------
Reporter: flesser | Owner: nobody
Type: Bug | Status: new
Component: Uncategorized | Version: 1.9
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------+--------------------
When using `date_format()` on a `datetime.date` object, I get the
following error:
{{{
File "<project>/<app>/models.py" in __str__
104. return date_format(self.date, format="%A, %d.%m.%Y")

File "<project>/env/lib/python3.4/site-packages/django/utils/formats.py"
in date_format
151. return dateformat.format(value, get_format(format or
'DATE_FORMAT', use_l10n=use_l10n))

File "<project>/env/lib/python3.4/site-
packages/django/utils/dateformat.py" in format
367. return df.format(format_string)

File "<project>/env/lib/python3.4/site-
packages/django/utils/dateformat.py" in format
37. pieces.append(force_text(getattr(self, piece)()))

File "<project>/env/lib/python3.4/site-
packages/django/utils/dateformat.py" in A
66. if self.data.hour > 11:

Exception Type: AttributeError at /
Exception Value: 'datetime.date' object has no attribute 'hour'
}}}

It seems to expect a `datetime.datetime`object, while the docstring for
`date_format()` explicitly mentions `datetime.date` as a valid input, too:
''"Formats a datetime.date or datetime.datetime object using a localizable
format"''.

'''How to reproduce:'''

models.py:
{{{
from django.db import models
from django.utils.formats import date_format

class ArrivalDate(models.Model):
date = models.DateField()

def __str__(self):
return date_format(self.date, format="%A, %d.%m.%Y")
}}}

template.html:
{{{
the date is {{ arrivaldate_instance }}
}}}

settings.py:
{{{
LANGUAGE_CODE = 'de-de'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_L10N = True
USE_TZ = True
}}}

I'm running Django 1.9.2 on Python 3.4

--
Ticket URL: <https://code.djangoproject.com/ticket/26281>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Feb 25, 2016, 10:53:28 AM2/25/16
to django-...@googlegroups.com
#26281: utils.formats.date_format() does not work with datetime.date
-------------------------------+--------------------------------------

Reporter: flesser | Owner: nobody
Type: Bug | Status: new
Component: Uncategorized | Version: 1.9
Severity: Normal | Resolution:
Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------+--------------------------------------
Changes (by flesser):

* cc: f.esser@… (added)
* needs_better_patch: => 0
* needs_tests: => 0
* needs_docs: => 0


--
Ticket URL: <https://code.djangoproject.com/ticket/26281#comment:1>

Django

unread,
Feb 25, 2016, 12:01:15 PM2/25/16
to django-...@googlegroups.com
#26281: Improve utils.formats.date_format() error message when using time
formatting with datetime.date
--------------------------------------+------------------------------------
Reporter: flesser | Owner: nobody
Type: Cleanup/optimization | Status: new
Component: Utilities | Version: 1.9
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted

Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
--------------------------------------+------------------------------------
Changes (by timgraham):

* component: Uncategorized => Utilities
* stage: Unreviewed => Accepted
* type: Bug => Cleanup/optimization


Comment:

You can't use `%A` (which corresponds to AM/PM) with a date. I guess you
were looking at Python's formatting characters where that corresponds to
"Weekday as locale’s full name".

Accepting on the basis that it might be feasible to improve the error
message.

--
Ticket URL: <https://code.djangoproject.com/ticket/26281#comment:2>

Django

unread,
Feb 26, 2016, 3:01:50 AM2/26/16
to django-...@googlegroups.com
#26281: Improve utils.formats.date_format() error message when using time
formatting with datetime.date
--------------------------------------+------------------------------------
Reporter: flesser | Owner: nobody

Type: Cleanup/optimization | Status: new
Component: Utilities | Version: 1.9
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
--------------------------------------+------------------------------------

Comment (by flesser):

I indeed was expecting to use Python's formatting characters in a Python
project.

Using PHP's formatting characters instead (`format="l, d.m.Y"`) as
[https://docs.djangoproject.com/es/1.9/ref/templates/builtins/#date
documented here] gives me the result I was looking for.

Considering the method is intended for use in a Twig-like template
environment, the design decision to make it PHP compatible seems
reasonable. But mentioning that in the docstring and the error message
would help to prevent misunderstandings like mine.

--
Ticket URL: <https://code.djangoproject.com/ticket/26281#comment:3>

Django

unread,
Apr 3, 2016, 4:39:53 AM4/3/16
to django-...@googlegroups.com
#26281: Improve utils.formats.date_format() error message when using time
formatting with datetime.date
--------------------------------------+------------------------------------
Reporter: flesser | Owner: benxy
Type: Cleanup/optimization | Status: assigned

Component: Utilities | Version: 1.9
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
--------------------------------------+------------------------------------
Changes (by benxy):

* owner: nobody => benxy
* status: new => assigned


--
Ticket URL: <https://code.djangoproject.com/ticket/26281#comment:4>

Django

unread,
Apr 5, 2016, 9:58:54 AM4/5/16
to django-...@googlegroups.com
#26281: Improve utils.formats.date_format() error message when using time
formatting with datetime.date
--------------------------------------+------------------------------------
Reporter: flesser | Owner: benxy
Type: Cleanup/optimization | Status: assigned
Component: Utilities | Version: 1.9
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
--------------------------------------+------------------------------------
Changes (by timgraham):

* has_patch: 0 => 1


Comment:

[https://github.com/django/django/pull/6407 PR]

--
Ticket URL: <https://code.djangoproject.com/ticket/26281#comment:5>

Django

unread,
Apr 5, 2016, 11:35:31 AM4/5/16
to django-...@googlegroups.com
#26281: Improve utils.formats.date_format() error message when using time
formatting with datetime.date
--------------------------------------+------------------------------------
Reporter: flesser | Owner: benxy
Type: Cleanup/optimization | Status: assigned
Component: Utilities | Version: 1.9
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 1

Easy pickings: 0 | UI/UX: 0
--------------------------------------+------------------------------------
Changes (by timgraham):

* needs_better_patch: 0 => 1


Comment:

Left some comments for improvement.

--
Ticket URL: <https://code.djangoproject.com/ticket/26281#comment:6>

Django

unread,
Apr 20, 2016, 8:21:36 PM4/20/16
to django-...@googlegroups.com
#26281: Improve utils.formats.date_format() error message when using time
formatting with datetime.date
--------------------------------------+------------------------------------
Reporter: flesser | Owner: benxy
Type: Cleanup/optimization | Status: closed
Component: Utilities | Version: 1.9
Severity: Normal | Resolution: fixed

Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 1
Easy pickings: 0 | UI/UX: 0
--------------------------------------+------------------------------------
Changes (by Tim Graham <timograham@…>):

* status: assigned => closed
* resolution: => fixed


Comment:

In [changeset:"45c7acdc507d26451f3c3f22b5d60bdd6d572cfc" 45c7acdc]:
{{{
#!CommitTicketReference repository=""
revision="45c7acdc507d26451f3c3f22b5d60bdd6d572cfc"
Fixed #26281 -- Added a helpful error message for an invalid format
specifier to dateformat.format().
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/26281#comment:7>

Django

unread,
Nov 22, 2019, 7:25:12 AM11/22/19
to django-...@googlegroups.com
#26281: Improve utils.formats.date_format() error message when using time
formatting with datetime.date
-------------------------------------+-------------------------------------
Reporter: Florian Eßer | Owner: Marko
Type: | Benko

Cleanup/optimization | Status: closed
Component: Utilities | Version: 1.9
Severity: Normal | Resolution: fixed
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 1
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Mariusz Felisiak <felisiak.mariusz@…>):

In [changeset:"76ec032712d28dd8d4b9ad425f2b330a3e9a9109" 76ec032]:
{{{
#!CommitTicketReference repository=""
revision="76ec032712d28dd8d4b9ad425f2b330a3e9a9109"
Refs #26281 -- Added a helpful error message for an invalid "r" specifier
to dateformat.format().
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/26281#comment:8>

Reply all
Reply to author
Forward
0 new messages