Code

Opened 3 years ago

Closed 20 months 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>

Attachments (0)

Change History (5)

comment:1 Changed 3 years ago by gabrielhurley

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Triage Stage changed from Unreviewed to Accepted

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 Changed 3 years ago by anonymous

  • Severity set to Normal
  • Type set to New feature

comment:3 Changed 2 years ago by aaugustin

  • UI/UX unset

Change UI/UX from NULL to False.

comment:4 Changed 2 years ago by aaugustin

  • Easy pickings unset

Change Easy pickings from NULL to False.

comment:5 Changed 20 months ago by timo

  • Resolution set to needsinfo
  • Status changed from new to closed

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

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.