Very sorry everybody: I googled around tons, but I didn't search this
group! LOL.  I'll know next time.

Adding an "invalid" choice to a ChoiceField?
http://groups.google.com/group/django-users/browse_thread/thread/dd6d8eb11f915c94/77aa6fbea94af4c4?lnk=gst&q=choicefield#77aa6fbea94af4c4

This works:

     colours = (('','Please choose a colour:'),('r','Red'),
('g','Green'),('b','Blue))
     # ...
     fave_colour = forms.ChoiceField(choices=colours)

Django's automagical form validation means the form cannot be
submitted without choosing a colour, and somehow (because empty string
always sorts before anything else?) the 'invalid' choice is displayed
by default and can act as a prompt.

Thanks (for the facility), apologies and best wishes

Ivan


On Mar 23, 2:56 pm, Ivan Uemlianin <i...@llaisdy.com> wrote:
> Dear All
>
> I have a choiceField in one of my forms, and I'd like to have it
> rendered displaying a prompt.  I think the html would be something
> like this:
>
>     <select>
>         <option value="">Please choose a colour:</option>
>         <option value="r">Red</option>
>         <option value="g">Green</option>
>         <option value="b">Blue</option>
>     </select>
>
> This would display a drop-down menu showing "Please choose a colour:".
>
> The form field would be something like this:
>
>     colours = (('r','Red'),('g','Green'),('b','Blue))
>     # ...
>     fave_colour = forms.ChoiceField(choices=colours)
>
> The problem is, this displays one of the choices.  I'd much prefer a
> prompt (in fact for our application, displaying one of the choices, as
> if it were a kind of 'default', is not at /all/ appropriate).
>
> I've tried
>
>     fave_colour = forms.ChoiceField(label="Please choose a colour:"
>                                     choices=colours)
>
> and
>
>     fave_colour = forms.ChoiceField(initial="Please choose a colour:"
>                                     choices=colours)
>
> and I've scoured the net for clues, but found nothing which works.
>
> Please can anyone here help me work out how to do this with
> django.forms?
>
> With thanks and best wishes
>
> Ivan

-- 
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.

Reply via email to