Changing tutorial radio button to drop down

15 views
Skip to first unread message

Max

unread,
Jan 6, 2009, 12:20:49 PM1/6/09
to Django users
Hello,

Using the tutorial for detail.view, I changed the radio button to a
drop down. I kept the view the same as the tutorial. See
http://dpaste.com/106018/

When I select the choice on the drop down, I receive the error saying
I didn't make a choice. I have a mistake in either the drop down
code or I need to change the view.

Can someone help?

Thanks,

Max

Karen Tracey

unread,
Jan 6, 2009, 12:36:22 PM1/6/09
to django...@googlegroups.com

You didn't put a name on the select element, thus its value is not included when the form is posted:

http://www.w3schools.com/TAGS/att_select_name.asp

You are also setting the id to choice.id before you have set choice to anything, so id will be blank I believe.  

Note if you use Django forms:

http://docs.djangoproject.com/en/dev/topics/forms/#topics-forms-index

you may spare yourself from having to hand-write a lot of this form stuff.  (Though it is good to learn some basics of it anyway.)

Karen

Daniel Roseman

unread,
Jan 6, 2009, 12:47:36 PM1/6/09
to Django users
On Jan 6, 12:20 pm, Max <adles...@gmail.com> wrote:
> Hello,
>
> Using the tutorial for detail.view, I changed the radio button to a
> drop down.  I kept the view the same as the tutorial.  Seehttp://dpaste.com/106018/
>
> When I select the choice on the drop down, I receive the error saying
> I didn't make a choice.   I have a mistake in either the drop down
> code or I need to change the view.
>
> Can someone help?
>
> Thanks,
>
> Max

The problem is in your template. Specifically, line 7:
<select id="{{ choice.id }}">
At this point, you don't have a variable called 'choice', as you don't
set that until the for loop in the next line. If you look at your
rendered HTML (do View Source within the browser), you'll see that
this will probably just give you this markup:
<select id="">
So, obviously, there's nothing to link the selected value with any key
in the POST dictionary.

Instead, you probably want this:
<select id='id_choice' name='choice'>
Note that you should really set a name as well as the id - that's the
best way of referring to form fields.

However, I have to say that the tutorial doesn't really give you the
best way of doing things here. Django has a great built-in forms
framework, which allows you to do the same thing in far less code and
much more 'Djangonically' (is that the right word?) and this bypasses
it completely. Unfortunately, the tutorial doesn't cover the forms
framework - although it is very well documented here:
http://docs.djangoproject.com/en/dev/topics/forms/

--
DR.

Max

unread,
Jan 6, 2009, 1:04:09 PM1/6/09
to Django users
Thanks!! It works now!

Max

Reply all
Reply to author
Forward
0 new messages