Electronics & Programming

develissimo

Open Source electronics development and programming

  • You are not logged in.
  • Root
  • » Django
  • » Displaying DB Values in a Dropdown Box [RSS Feed]

#1 Jan. 5, 2011 21:27:58

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

Displaying DB Values in a Dropdown Box


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.



--
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 Jan. 5, 2011 21:59:12

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

Displaying DB Values in a Dropdown Box


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.http://docs.djangoproject.com/en/1.2/intro/tutorial01/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.http://docs.djangoproject.com/en/1.2/topics/forms/modelforms/Shawn

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

#3 Jan. 5, 2011 22:21:26

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

Displaying DB Values in a Dropdown Box


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.

On Jan 5, 3:59 pm, Shawn Milochik <sh...@milochik.com> wrote:
> 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.
>
>http://docs.djangoproject.com/en/1.2/intro/tutorial01/>
> 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.
>
>http://docs.djangoproject.com/en/1.2/topics/forms/modelforms/>
> Shawn

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

#4 Jan. 6, 2011 02:37:07

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

Displaying DB Values in a Dropdown Box


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:http://docs.djangoproject.com/en/1.2/topics/forms/modelforms/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

--
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
  • » Displaying DB Values in a Dropdown Box [RSS Feed]

Board footer

Moderator control

Enjoy the 24th 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