Skip to content

Instantly share code, notes, and snippets.

@zen4ever
Created September 25, 2011 21:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zen4ever/1241198 to your computer and use it in GitHub Desktop.
Save zen4ever/1241198 to your computer and use it in GitHub Desktop.
Hour widget django
from django import forms
TIME_CHOICES = [('', '--------')]
for i in range(0, 24):
for m in ["00", "30"]:
hour = i % 12
if i == 12:
hour = 12
TIME_CHOICES.append(
("%02d:%s" % (i, m),
"%02d:%s " % (hour, m) + ((i < 12) and "am" or "pm")))
class SelectTimeInput(forms.Select):
format = '%H:%M'
def __init__(self, attrs=None, choices=()):
if not choices:
choices = TIME_CHOICES
else:
choices = list(choices)
super(SelectTimeInput, self).__init__(attrs, choices)
def render_options(self, choices, selected_choices):
def convert_choices(v):
if hasattr(v, 'strftime'):
v = v.strftime(self.format)
return v
selected_choices = map(convert_choices, selected_choices)
return super(SelectTimeInput, self).render_options(
choices,
selected_choices)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment