Re: Django Form ChoiceField, initial not working
On Dec 11, 5:08 am, Sævar Öfjörð wrote: > I think you should provide your choices in the form field, not when > creating an instance of the form. Agreed. To create a dynamic form such as this, the best way would probably be to use a custom __init__ function. In your view, when you create your form, you can pass in a 'queryset=' argument that will do this for you. Wayne -- 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: Django Form ChoiceField, initial not working
I think you should provide your choices in the form field, not when creating an instance of the form. The form would then be something like this: class StoreLocationHoursForm(BForm): sl_list= [('-', 'Choose Location'), (u'Super City', u'901 PHILADELPHIA STREET')] location = forms.ChoiceField(choices=sl_list, required=True) # rest of fields... The initial keyword when creating an instance of the form is used to select which of the choices is rendered as selected. Haven't tested this though. - Sævar On Dec 9, 11:18 pm, Sector7B wrote: > Hi, i have a simple form: > class StoreLocationHoursForm(BForm): > location = forms.ChoiceField(required=True) > day = forms.ChoiceField(choices=bagit_constants.dow,required=True) > time_open_hours = > forms.ChoiceField(choices=constants.hours,required=True) > time_open_minutes = > forms.ChoiceField(choices=constants.minutes,required=True) > time_close_hours = > forms.ChoiceField(choices=constants.hours,required=True) > time_close_minutes = > forms.ChoiceField(choices=constants.minutes,required=True) > > there can be many locations, so in my view i loop through them to > create a list of tuples to provide a choices list for location: > Log Output of the list when im done:sl_list= [('-', 'Choose > Location'), (u'Super City', u'901 PHILADELPHIA STREET')] > > the code i use to set initial is the following: > > form = StoreLocationHoursForm(initial={"location":sl_list}) > > However my select is empty when it renders the HTML. > > Thanks for the help in advance, > > j -- 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.
Django Form ChoiceField, initial not working
Hi, i have a simple form: class StoreLocationHoursForm(BForm): location = forms.ChoiceField(required=True) day = forms.ChoiceField(choices=bagit_constants.dow,required=True) time_open_hours = forms.ChoiceField(choices=constants.hours,required=True) time_open_minutes = forms.ChoiceField(choices=constants.minutes,required=True) time_close_hours = forms.ChoiceField(choices=constants.hours,required=True) time_close_minutes = forms.ChoiceField(choices=constants.minutes,required=True) there can be many locations, so in my view i loop through them to create a list of tuples to provide a choices list for location: Log Output of the list when im done:sl_list= [('-', 'Choose Location'), (u'Super City', u'901 PHILADELPHIA STREET')] the code i use to set initial is the following: form = StoreLocationHoursForm(initial={"location":sl_list}) However my select is empty when it renders the HTML. Thanks for the help in advance, j -- 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.