Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What should "value_from_datadict" method of a custom form widget return?

Tags:

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?

like image 715
MikeN Avatar asked Jan 12 '09 20:01

MikeN


1 Answers

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))
like image 139
Jeff Bauer Avatar answered Sep 30 '22 17:09

Jeff Bauer