#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)
Change History (12)
comment:1 by , 17 years ago
| milestone: | → 1.1 | 
|---|---|
| Triage Stage: | Unreviewed → Accepted | 
comment:2 by , 17 years ago
| Cc: | added | 
|---|
comment:3 by , 17 years ago
| Keywords: | pycamp2009 added | 
|---|
comment:4 by , 17 years ago
| Owner: | changed from to | 
|---|
by , 17 years ago
| Attachment: | datefilter_t9520_r10177.diff added | 
|---|
comment:5 by , 17 years ago
| Has patch: | set | 
|---|
by , 17 years ago
| Attachment: | 9520-with-tests.diff added | 
|---|
comment:6 by , 17 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 , 17 years ago
| Owner: | changed from to | 
|---|---|
| Status: | new → assigned | 
comment:8 by , 17 years ago
| Resolution: | → fixed | 
|---|---|
| Status: | assigned → closed | 
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.