Opened 16 years ago

Closed 16 years ago

Last modified 13 years ago

#9520 closed (fixed)

date filter should not crash when dealing with non date objects

Reported by: hotani Owned by: Eric Holscher
Component: Template system Version: dev
Severity: Keywords: date format filter template pycamp2009
Cc: Nicolás Miyasato Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

I'm using date filter on a form field like so: {{ form.field.data|date:"m-d-Y" }} When loading the form for editing, all is well. However, if validation fails for the form, date fields are sent back as unicode and not date objects. The date filter throws an error.

As a workaround, I wrote a wrapper for the date filter which puts it in a try/except block, returning the value if 'date' fails. This works but feels hacky.

Attachments (2)

datefilter_t9520_r10177.diff (784 bytes ) - added by Andrew Badr 16 years ago.
9520-with-tests.diff (1.7 KB ) - added by Eric Holscher 16 years ago.

Download all attachments as: .zip

Change History (12)

comment:1 by Jacob, 16 years ago

milestone: 1.1
Triage Stage: UnreviewedAccepted

comment:2 by Nicolás Miyasato, 16 years ago

Cc: Nicolás Miyasato added

comment:3 by Nicolás Miyasato, 16 years ago

Keywords: pycamp2009 added

comment:4 by Andrew Badr, 16 years ago

Owner: changed from nobody to Andrew Badr

by Andrew Badr, 16 years ago

comment:5 by Andrew Badr, 16 years ago

Has patch: set

The attachment implements the behavior described in the ticket, returning value if there is an exception. It patches the time filter in addition to date since the two cases are directly analogous. Only catches AttributeError since that's what you get when you pass a string to format or time_format. I'd prefer to raise an exception if value can't be formatted and is not a basestring.

by Eric Holscher, 16 years ago

Attachment: 9520-with-tests.diff added

comment:6 by Eric Holscher, 16 years ago

Added simple tests of the date filter. Also made it so that the filter returns the empty string on error, instead of the original value, which is a Django convention.

comment:7 by Eric Holscher, 16 years ago

Owner: changed from Andrew Badr to Eric Holscher
Status: newassigned

comment:8 by Jacob, 16 years ago

Resolution: fixed
Status: assignedclosed

(In [10365]) Fixed #9520: make the date filter fail silently for non-date values. Thanks, Andrew Badr and Eric Holscher.

comment:9 by Jacob, 16 years ago

(In [10366]) [1.0.X] Fixed #9520: make the date filter fail silently for non-date values. Thanks, Andrew Badr and Eric Holscher. Backport of r10365 from trunk.

comment:10 by Jacob, 13 years ago

milestone: 1.1

Milestone 1.1 deleted

Note: See TracTickets for help on using tickets.
Back to Top