views:

689

answers:

2

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?

+2  A: 

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))
Jeff Bauer
A: 

The Django source says

Given a dictionary of data and this widget's name, returns the value of this widget. Returns None if it's not provided.

Reading the code, I see that Django's separate Date and Time widgets are both subclasses of Input, subclasses of Widget, which appears to work with simple Strings.

S.Lott