#3533: Widget to format dates/times
---------------------------+------------------------------------------------
Reporter: SmileyChris | Owner: adrian
Status: new | Component: django.newforms
Version: SVN | Resolution:
Keywords: | Stage: Design decision needed
Has_patch: 1 | Needs_docs: 0
Needs_tests: 0 | Needs_better_patch: 0
---------------------------+------------------------------------------------
Comment (by jkocherhans):
For what it's worth, I've done the same thing, but just wrote a custom
widget. My use case was to avoid times being rendered as 23:30:00 for
instance. Seconds are irrelevant and I want to display a 12 hours clock
with AM/PM. Something more like 9:00 PM. Currently, newforms will render
that as 21:00:00. It doesn't matter what formats the widget accepts, it
always renders them the same by basically calling str(value)
{{{
#!python
class TimeWidget(forms.TextInput):
def render(self, name, value, attrs=None):
# Midnight, time(0, 0) is False. Check for that in addition to an
*actual* missing value.
if value or value == time(0, 0):
value = value.strftime("%I:%M %p")
# Strip off leading 0's. Should be part of strftime, but
whatever.
if value[0] == '0': value = value[1:]
return super(TimeWidget, self).render(name, value, attrs)
}}}
--
Ticket URL: <http://code.djangoproject.com/ticket/3533#comment:3>
Django Code <http://code.djangoproject.com/>
The web framework for perfectionists with deadlines
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/django-updates?hl=en
-~----------~----~----~----~------~----~------~--~---