#7499 closed (fixed)
TimeField may contain the sub-second times but cannot validate them
| Reported by: | honeyman | Owned by: | Kevin McConnell |
|---|---|---|---|
| Component: | Forms | Version: | dev |
| Severity: | Keywords: | time aug22sprint | |
| Cc: | kevin.mcconnell@… | Triage Stage: | Ready for checkin |
| Has patch: | yes | Needs documentation: | no |
| Needs tests: | no | Patch needs improvement: | no |
| Easy pickings: | no | UI/UX: | no |
Description
If we create a form containing a TimeField which has the initial value set to time object which contains the sub-second data (for example, to time.max), the form is generated with the value which cannot be validated.
Example:
>>> from django import newforms as forms
>>> from datetime import time
>>>
>>> class MyForm(forms.Form):
... mytime = forms.TimeField(initial = time.max)
...
>>> f1 = MyForm()
>>> # Imagine we've posted this form and are now parsing the POST request
... f1a = MyForm({'mytime': str(time.max)})
>>>
>>> print f1
<tr><th><label for="id_mytime">Mytime:</label></th><td><input type="text" name="mytime" value="23:59
:59.999999" id="id_mytime" /></td></tr>
>>>
>>> print f1a
<tr><th><label for="id_mytime">Mytime:</label></th><td><ul class="errorlist"><li>Enter a valid time.
</li></ul><input type="text" name="mytime" value="23:59:59.999999" id="id_mytime" /></td></tr>
>>>
>>> print f1a.is_bound, f1a.is_valid()
True False
As Django (Python) cannot parse the sub-second time in the text string, my understanding is that Django should never print it in the field, trimming the value to just '%H:%M:%S'.
This is likely applicable to DateTimeField too.
Attachments (3)
Change History (14)
comment:1 by , 17 years ago
| milestone: | → 1.0 maybe |
|---|---|
| Triage Stage: | Unreviewed → Design decision needed |
comment:2 by , 17 years ago
| milestone: | 1.0 maybe → 1.0 |
|---|---|
| Triage Stage: | Design decision needed → Accepted |
comment:3 by , 17 years ago
| Owner: | changed from to |
|---|---|
| Status: | new → assigned |
by , 17 years ago
| Attachment: | time_input_widget.diff added |
|---|
comment:4 by , 17 years ago
| Cc: | added |
|---|---|
| Has patch: | set |
| Keywords: | aug22sprint added |
The patch adds a TimeInput widget, which functions similarly to DateTimeInput but with a suitable format string.
comment:5 by , 17 years ago
After discussion with Malcolm, I've updated the patch to truncate the time down to seconds (using replace(microsecond=0)) rather than using the format string (which wasn't quite so explicit).
comment:7 by , 17 years ago
| Resolution: | fixed |
|---|---|
| Status: | closed → reopened |
I almost forgot about this ticket, but it hit me again just now...
After an "svn update" of Django, I just got "Caught an exception while rendering: replace() takes no keyword arguments" in /forms/widgets.py, line 311, in render "value = value.replace(microsecond=0)". Seems that I was trying to initialize this widget using an unicode string (rather than a datetime.time), and it worked perfectly before; but now it fails (cause string object also does have a replace() method, with a different syntax though). Should it be considered a regression, or does such widget now have to be initialized with datetime.time since now?
comment:8 by , 17 years ago
That's a good point, I didn't think about directly instantiating the widgets like that.
Perhaps it would be safer to check the type of the argument (since replace could be a common method name)? I know some folks have strong feelings about doing that in Python, but it seems like it would be safer.
I'll attach a new patch that does that.
by , 17 years ago
| Attachment: | time_input_widget.3.diff added |
|---|
Check TimeInput argument type (patched against r8507)
comment:9 by , 17 years ago
| Triage Stage: | Accepted → Ready for checkin |
|---|
comment:10 by , 17 years ago
| Resolution: | → fixed |
|---|---|
| Status: | reopened → closed |
Adds a TimeInput widget