Opened 14 years ago

Closed 12 years ago

#14769 closed New feature (needsinfo)

Voting example from tutorial - use of forms API - suggestion

Reported by: bradders Owned by: nobody
Component: Documentation Version: 1.2
Severity: Normal Keywords:
Cc: Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

I'm a django newbie working through the tutorials and documentation to get me going for a project.

I built the simple Poll app - very useful introduction. I then wanted to learn the Forms API, and it seemed an obvious learning exercise to implement the voting form using the API. Turned out to be much harder than I expected - perhaps I haven't used the most appropriate technique - but can I suggest you include an implementation (happy for you to use mine) within the "working with forms" introduction to keep people like me sane.

My solution is below. I was using Google App Engine so you'll see use of object keys instead of ids and other limitations of app engine that I'm sure you could easily turn into proper django.

Implement a forms.VotingForm class:

class VotingForm(forms.Form):
    # this needs to be hidden    
    poll_key = forms.CharField(widget=forms.HiddenInput)
    
    # we need to add this choice dynamically at run time
    def __init__(self, vote_choices, *args, **kwargs):
        super(VotingForm, self).__init__(*args, **kwargs)
        
        form_vote_choices = []
        for vote_choice in vote_choices:
            form_vote_choices.append((vote_choice.key(),vote_choice.choice))  
    
        self.fields['choice']= forms.ChoiceField(label='Your vote',
                                   widget=forms.RadioSelect,
                                   choices=form_vote_choices)       

Change the poll.views.poll_detail function to populate the form:

def poll_detail(request, poll_key):
    
    aPoll = Poll.get(poll_key)
    aVotingForm = VotingForm(aPoll.choice_set)
    if aPoll:
        
        return render_to_response('polls/poll_detail.html',
            {'poll':aPoll,'voting_form':aVotingForm})
    else:
     
        raise Http404

And the html:

<form action="/polls/{{ poll.key }}/vote/" method="post">

{{ voting_form.as_p }}
<input type="submit" value="Vote" />

</form>

Change History (5)

comment:1 by Gabriel Hurley, 14 years ago

Triage Stage: UnreviewedAccepted

On the one hand, I agree with bradders about it being a natural next step to try and create a form for the polls app. Unfortunately, the polls app is not designed in a way that making a form for it is simple.

On the other hand, this is a relatively advanced topic, and also somewhat of an uncommon case (in which you're creating a form to select among the items in a reverse relationship), so I don't see it belonging in either the tutorial or the docs/topics/forms overview.

It does seem like some documentation on modifying forms at runtime would be handy, since glancing through all the form docs I don't see any great examples.

I'm not sold on this ticket, but pending a real patch I'm alright with the idea.

comment:2 by anonymous, 14 years ago

Severity: Normal
Type: New feature

comment:3 by Aymeric Augustin, 13 years ago

UI/UX: unset

Change UI/UX from NULL to False.

comment:4 by Aymeric Augustin, 13 years ago

Easy pickings: unset

Change Easy pickings from NULL to False.

comment:5 by Tim Graham, 12 years ago

Resolution: needsinfo
Status: newclosed

Agreed with Gabriel, if we had a patch it might be worth considering.

Note: See TracTickets for help on using tickets.
Back to Top