#26281 closed Cleanup/optimization (fixed)
Improve utils.formats.date_format() error message when using time formatting with datetime.date
Reported by: | Florian Eßer | Owned by: | Marko Benko |
---|---|---|---|
Component: | Utilities | Version: | 1.9 |
Severity: | Normal | Keywords: | |
Cc: | f.esser@… | Triage Stage: | Accepted |
Has patch: | yes | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | yes |
Easy pickings: | no | UI/UX: | no |
Description
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
Change History (8)
comment:1 by , 9 years ago
Cc: | added |
---|
comment:2 by , 9 years ago
Component: | Uncategorized → Utilities |
---|---|
Summary: | utils.formats.date_format() does not work with datetime.date → Improve utils.formats.date_format() error message when using time formatting with datetime.date |
Triage Stage: | Unreviewed → Accepted |
Type: | Bug → Cleanup/optimization |
comment:3 by , 9 years ago
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 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.
comment:4 by , 9 years ago
Owner: | changed from | to
---|---|
Status: | new → assigned |
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.