On Monday, January 10, 2011 2:56:42 PM UTC, hank23 wrote: > > This is my first attempt at trying to code a form and use it in a > view, so I've probably got something wrong. At this point I just > wanted to produce the screen with all of the fields displayed on it, > just to see if I have things coded correctly to display the screen the > first time. > > I'm not getting any error but I'm also not getting everything on my > screen which I think I should be getting. The screen should display a > title, then a message, a dropdown box(pollquestions), a button, and > finally another message. At least it should as long as I have my html > coded properly. My form is defined this way: > > class AddChoiceForm(forms.Form): > infomessage = forms.CharField(max_length=200) > pollquestions = > forms.ModelChoiceField(queryset=Poll.objects.all(), required=True, > label='Poll Questions', initial='Please select a poll question', > help_text='Select a poll question.', > error_messages={'required': 'Please select a poll question!', > 'invalid_choice': 'Cannot select initial value!'}, > widget=forms.Select(attrs={'name': 'pollquestions', > 'id':'pollquestions', 'size':'1', 'type':'select-one' })) > confirmmessage = forms.CharField(max_length=200) > newchoice = forms.CharField(max_length=200) > > > my addchoice.html screen is defined this way: > > <h1>Add Poll Choice Screen</h1> > > <form action="/polls/addchoice/" method="POST"> > {% csrf_token %} > > {{ infomessage }} > > <br /><br /> > > {{ pollquestions }} > > <br /><br /> > <input type="submit" value="addchoice" /> > <br /><br /> > > {{ confirmmessage }} > > </form> > > and my view is coded this way: > > def addchoice(request): > polls = Poll.objects.all() > 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." > > confirm = "Nothing added." > > data = {'pollquestions': polls, 'infomessage': message, > 'confirmmessage': confirm } > form = AddChoiceForm(data) > dctnry = {'form': form, 'data': data} > return render_to_response('polls/addchoice.html', > dctnry, > > context_instance=RequestContext(request)) > > > I would appreciate any help or suggestions, for figuring this out. > Thanks. >
There are a number of things wrong here. You've got two things called 'pollquestions'. One is a field in the form. Since you're passing the form into the template context with the key 'form', you access it in the template as {{ form.pollquestions }}. The other thing called 'pollquestions' is a list of all the polls in your database. You've put that inside a dictionary which you're passing into the context under the key 'data', so if you wanted to access that, you'd do {{ data.pollquestions }} - or, more likely, you'd want to iterate through with {% for poll in data.pollquestions %}. However I suspect you are only interested in the form field here. Similarly, you've got two things called 'infomessage' and 'confirmmessage' - one is a field, and one is a string with a message. This time, I suspect you are interested in the strings - which again are accessed via {{ data.infomessage }} and {{ data.confirmmessage }}. If you don't want the user to actually be able to enter these, don't define them as fields in the form - you can reference them directly in the template as I have shown. Next, this line: form = AddChoiceForm(data) should only be used when you have data posted from the user. Since you're not doing that yet, just use form = AddChoiceForm() Finally, you're probably overspecifying the pollquestions field in the form. Most of what you have is the default anyway, and other things are invalid: for example, you can't specify 'initial' as text, it has to be the ID of one of the possible options. Just use: pollquestions = forms.ModelChoiceField(queryset=Poll.objects.all(), required=True, label='Poll Questions', help_text='Select a poll question.') -- DR. -- 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.