Displaying DB Values in a Dropdown Box

1,238 views
Skip to first unread message

hank23

unread,
Jan 5, 2011, 4:27:50 PM1/5/11
to Django users
I'm trying to display DB data in a dropdon box on a screen using a
form. Here's my form/formfield code:

from django import forms

class AddChoiceForm(forms.Form):
ccount = forms.IntegerField()
pquestion = forms.CharField()
message = forms.CharField()
polls = forms.ChoiceField(required=True)
newchoice = forms.CharField()

and here's the html code for my screen:

<h1>Add Poll Choice Screen</h1>

<form action="/polls/addchoice/" method="POST">
{% csrf_token %}

{{ message }}

<br /><br />
{{ polls }}
<br /><br />
<input type="submit" value="addchoice" />
<br /><br />

</form>

and finally here's the view code for addchoice:

def addchoice(request):
polls = get_list_or_404(Poll)
pollcount = Poll.objects.all().count()
if pollcount > 0 :
message = "Number of polls passed to form was " + str(pollcount)
else:
message = "No polls passed in to form."

data = {'polls': polls, 'message': message }
form = AddChoiceForm(data)
return render_to_response('polls/addchoice.html',
{ 'form': form })

I had expected this to just display the message field on my screen and
the DB entries in the drop down, but when the screen is rendered I'm
only seeing the screen title at the top and the button near the
bottom. So where's problem with the code? Or is there more default
widget coding I need to do to setup the widget for the choice field
which will contain the DB values? Please advise. Thanks.



Shawn Milochik

unread,
Jan 5, 2011, 4:59:00 PM1/5/11
to django...@googlegroups.com
You're not seeing anything for several reasons. There are just a lot of fundamental things about the way Django works that it seems you haven't gotten to yet in your learning. For now, I recommend you follow the tutorial -- do everything step by step so you can see how it works. That alone will probably help you fix your code.


Once you've done that, you're going to want to make a ModelForm instead of a Form, and then create one for each instance of your Poll class and put them into a list, or use model formsets. You can read about all that stuff in the link below, but I think it will be overwhelming until you've gone through the tutorial.


Shawn

hank23

unread,
Jan 5, 2011, 5:21:18 PM1/5/11
to Django users
Actually I have been through the tutorial, but there are still a lot
of gaps in the information presented, because many of the examples may
or may not show all of the code in all of the various modules which is
needed for everything to work together successfully. My next question
is what is wrong with this form field definition in my form:

polls = forms.ChoiceField(required=True, widget=forms.Select,
choices=polls)

It seems to be having a problem with my specified "choices" valule. In
my view I plan on creating a list object from the database table which
will contain the choices to be displayed in my select (dropdown) box.
but the documentation doesn't seem to explicitly explain with a coding
example how to specify a database table column for the choices
parameter.

Shawn Milochik

unread,
Jan 5, 2011, 9:36:55 PM1/5/11
to django...@googlegroups.com
A "choices" value should be a tuple or list containing two-value tuple or list containing the key and value for each dropdown option.

Other issues with the code you originally posted:

1. What looked like a context (that dictionary containing "polls"), was being passed to the creation of a form, but then the template attempted to access the 'polls' object, which wouldn't exist there because it was never sent in the context passed to render_to_respone.

2. A form was being used, not a model form.

3. All polls were pulled from the model, but only one form was created. 

4. Even if multiple forms were created, it still wouldn't work because a list of models were being passed to the form instead of just one at a time (to create multiple forms).

5. Even if #4 was done correctly, it was being sent as the 'data' kwarg and not the 'instance' kwarg.

6. Even if all the rest was correct it still wouldn't have worked because it was a form instead of a model form in the first place.

Those reasons are why I suggested you go through the tutorial, because the code you posted is pretty much completely wrong. I was trying to be nice and not point out that it was hopelessly broken by suggesting you do the tutorial -- not just read it. It is kind of dated, but you'll see the manipulation of model instances within a template on part three, which you need because your {{ polls }} won't do what you want it to do.

Then, see the modelforms documentation which I linked to last time, and will again here:

I agree that the tutorial isn't comprehensive, and feel free to reply if you get stuck. It's just that it's a lot easier to help you if you're doing the right thing the wrong way instead of a big pile of wrong.

Shawn
Reply all
Reply to author
Forward
0 new messages