I like it :-)  But I'm also running into problems... how do you get
data to populate subwidgets?  I'm trying to create a Date widget with
dropdown lists for days, months, and years similar to the one I created
here: http://www.eborger.no/hifm/login.html (feel free to play around,
it's written in php but should still be pretty solid ;-).  What I did
there was update the selection widgets from post data, and when all of
them had values selected update a hidden field with the text version of
the value.

Here's what I've got so far:

First a quick-n-dirty select widget:

from django.newforms import *
from django.utils.html import escape
from datetime import date

def flatten(d):
    return u' '.join('%s="%s"' % (k, escape(v)) for k, v in d.items())

class SelectWidget(Widget):
    def __init__(self, options, selected=None):
        super(SelectWidget, self).__init__()
        self.options = options
        self.selected = selected

    def render(self, name, value, attrs=None):
        if value is not None:
            self.selected = value
        final_attrs = dict(self.attrs, name=name)
        if attrs:
            final_attrs.update(attrs)
        res = u'<select %s>' % flatten(final_attrs)
        for option in self.options:
            if option == self.selected:
                res += u'<option selected value="%s">%s</option>' %
(option, option)
            else:
                res += u'<option>%s</option>' % option
        res += u'</select>'
        return res

then for the date widget. At the point marked HERE I need data that I
don't seem to be able to access...?

class MyDateWidget(Widget):
    def render(self, name, value, attrs=None):
        if value is None or value == '':
            value = ''
            d = POST[name+u'_day']   # <=== HERE
            m = POST[name+u'_month']
            y = POST[name+u'_year']
        else:
            d,m,y = value.split('/')

        days = SelectWidget(range(1,32))
        months = SelectWidget(range(1,12))
        year = SelectWidget(range(2000, 2007))

        slash = u'&nbsp;/&nbsp;'
        res = '<input type=hidden value="%s" />' % value
        res += days.render(name+u'_day', d)
        res += slash
        res += months.render(name+u'_month', m)
        res += slash
        res += year.render(name+u'_year', y)

        return res

Did I miss something?

I also wrote up what I got from reading the code and tests at
http://blog.tkbe.org/archive/django-looking-at-newforms/

-- bjorn


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django developers" 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-developers
-~----------~----~----~----~------~----~------~--~---

Reply via email to