I'm trying to build my own custom django form widgets (putting them in widgets.py of my project directory). What should the value "value_from_datadict()" return? Is it returning a string or the actual expected value of the field?
I'm building my own version of a split date/time widget using JQuery objects, what should each part of the widget return? Should the date widget return a datetime and the time widget return a datetime? What glue code merges the two values together?
For value_from_datadict()
you want to return the value you expect or None. The source in django/forms/widgets.py provides some examples.
But you should be able to build a DatePicker widget by just providing a render
method:
DATE_FORMAT = '%m/%d/%y'
class DatePickerWidget(widgets.Widget):
def render(self, name, value, attrs=None):
if value is None:
vstr = ''
elif hasattr(value, 'strftime'):
vstr = datetime_safe.new_datetime(value).strftime(DATE_FORMAT)
else:
vstr = value
id = "id_%s" % name
args = [
"<input type=\"text\" value=\"%s\" name=\"%s\" id=\"%s\" />" % \
(vstr, name, id),
"<script type=\"text/javascript\">$(\"#%s\").datepicker({dateFormat:'mm/dd/y'});</script>" % id
]
return mark_safe("\n".join(args))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With