Electronics & Programming

develissimo

Open Source electronics development and programming

  • You are not logged in.
  • Root
  • » Django
  • » Forms - Widget - Radioselect - how to set initial value on a static form [RSS Feed]

#1 June 23, 2010 04:37:59

Karen T.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Forms - Widget - Radioselect - how to set initial value on a static form


On Tue, Jun 22, 2010 at 9:46 PM, Roboto <robc...@gmail.com> wrote:

> I can't seem to find documentation on this anywhere, so I imagine
> someone must have run into this.
>
> if I had a radioselect Yes / No and I wanted the default value to be
> 'No', but I'm not loading this data from a database or anything, is
> there a method to just insert an initial value so that the html comes
> out as <option selected = True>No</option>
>
> ?
>
> Any ideas? This is so basic it's killing me.
>

You say RadioSelect widget but you mention HTML that doesn't go with a radio
input type -- option goes with a <SELECT ...> construct? If you showed some
code for what you are trying it might help people help you. The way to
specify the initially-selection option for a RadioSelect is to specify it as
the initial value for the associated form field. For example:

class F1(forms.Form):
rs_field = forms.ChoiceField(choices=(('Y','Yes'), ('N','No')),
initial='N', widget=forms.RadioSelect)

which would render (unbound) as:

<tr><th><label for="id_rs_field_0">Rs field:</label></th><td><ul>
<li><label for="id_rs_field_0"><input type="radio" id="id_rs_field_0"
value="Y" name="rs_field" /> Yes</label></li>
<li><label for="id_rs_field_1"><input checked="checked" type="radio"
id="id_rs_field_1" value="N" name="rs_field" /> No<
/label></li>
</ul></td></tr>

...with the "No" radio input initially selected, since it specified the
checked attribute.

Karen
--http://tracey.org/kmt/--
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 athttp://groups.google.com/group/django-users?hl=en.

Offline

#2 June 23, 2010 09:12:53

e.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Forms - Widget - Radioselect - how to set initial value on a static form


Karen's suggestion is great. You can also pass initial data into the
form constructor. So using the example above if you do (in your view):

my_form = F1(initial=dict(rs_field='N'))

you'll achieve the same result. Don't think there's much to be said
for either way other the way I just suggest might allow you a bit more
flexibility if you want something in the view to dictate the initial
value.

Euan

On Jun 23, 4:37 am, Karen Tracey <kmtra...@gmail.com> wrote:
> On Tue, Jun 22, 2010 at 9:46 PM, Roboto <robc...@gmail.com> wrote:
> > I can't seem to find documentation on this anywhere, so I imagine
> > someone must have run into this.
>
> > if I had a radioselect Yes / No and I wanted the default value to be
> > 'No', but I'm not loading this data from a database or anything, is
> > there a method to just insert an initial value so that the html comes
> > out as <option selected = True>No</option>
>
> > ?
>
> > Any ideas? This is so basic it's killing me.
>
> You say RadioSelect widget but you mention HTML that doesn't go with a radio
> input type -- option goes with a <SELECT ...> construct? If you showed some
> code for what you are trying it might help people help you. The way to
> specify the initially-selection option for a RadioSelect is to specify it as
> the initial value for the associated form field. For example:
>
> class F1(forms.Form):
>     rs_field = forms.ChoiceField(choices=(('Y','Yes'), ('N','No')),
>         initial='N', widget=forms.RadioSelect)
>
> which would render (unbound) as:
>
> <tr><th><label for="id_rs_field_0">Rs field:</label></th><td><ul>
> <li><label for="id_rs_field_0"><input type="radio" id="id_rs_field_0"
> value="Y" name="rs_field" /> Yes</label></li>
> <li><label for="id_rs_field_1"><input checked="checked" type="radio"
> id="id_rs_field_1" value="N" name="rs_field" /> No<
> /label></li>
> </ul></td></tr>
>
> ...with the "No" radio input initially selected, since it specified the
> checked attribute.
>
> Karen
> --http://tracey.org/kmt/--
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 athttp://groups.google.com/group/django-users?hl=en.

Offline

  • Root
  • » Django
  • » Forms - Widget - Radioselect - how to set initial value on a static form [RSS Feed]

Board footer

Moderator control

Enjoy the 18th of October
PoweredBy

The Forums are managed by develissimo stuff members, if you find any issues or misplaced content please help us to fix it. Thank you! Tell us via Contact Options
Leave a Message
Welcome to Develissimo Live Support