Need help setting up dynamic options for select (drop down) box of form.
I'm trying to make the options of a particular drop down box be determined at runtime. I added a __init__ constructor to my django.forms.Form subclass as follows... def __init__(self, data= None, error_class = django.forms.util.ErrorList, manage_sites_domain = None): """ constructor """ super(InputPostsForm, self).__init__(data, error_class = error_class) if manage_sites_domain: domain = manage_sites_domain site = SITE.objects.get(domain = domain) categories = CATEGORY.objects.filter(site = site) choices= [2 * (e.name,) for e in categories] pc = self.fields["post_category"] pc.widget = SELECT() pc.choices = choices I printed the choices list to verify the list contains a tuple with 2 strings. When I view the form in a browser I see a drop down box with NO choices. Or rather, a blank choice. Chris -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-us...@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Re: Need help setting up dynamic options for select (drop down) box of form.
On Aug 3, 5:04 am, Chris Seberino wrote: > I'm trying to make the options of a particular drop down box be > determined at runtime. > > I added a __init__ constructor to my django.forms.Form subclass as > follows... > > def __init__(self, > data = None, > error_class = > django.forms.util.ErrorList, > manage_sites_domain = None): > > """ > > constructor > """ > > super(InputPostsForm, self).__init__(data, > error_class = > error_class) > if manage_sites_domain: > domain = manage_sites_domain > site = SITE.objects.get(domain = domain) > categories = CATEGORY.objects.filter(site = > site) > choices = [2 * (e.name,) for e in categories] > pc = self.fields["post_category"] > pc.widget = SELECT() > pc.choices = choices > > I printed the choices list to verify the list contains a tuple with 2 > strings. > > When I view the form in a browser I see a drop down box with NO > choices. Or rather, a blank choice. > > Chris I can't read this code at all. What's with all the spaces everywhere? Why are you using BLOCK CAPITALS for class names? Anyway, I suspect the cause is setting the widget. I've no idea what the SELECT widget does - it's not the same as the built-in Select widget, since Python is case-sensitive - but I would try without that and see if it now works. If not, please post the code of that widget class. -- DR. -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-us...@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Re: Need help setting up dynamic options for select (drop down) box of form.
On Aug 3, 3:29 am, Daniel Roseman wrote: > Anyway, I suspect the cause is setting the widget. I found my bug. To convert a textbox to a drop down box, it isn't enough to reset the widget and choices settings. It is also necessary of course to reset the field type from a CharField to a ChoicesField. All the best, Chris -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-us...@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.