I'd like to create a front facing poll creation form that can
automatically handle deletion, reordering of choices.

I believe formsets are the easiest way to accomplish this. I start
with 3 choices and want to allow the user to add more.

Check the following code:

def create(request):

        numchoices = 3 #default number of choices to display

        if request.method == "POST":

                questionform = QuestionForm(request.POST, instance=Question(),
prefix='question')
                ChoiceFormSet = formset_factory(ChoiceForm, 
extra=int(request.POST
['numchoices']), max_num=10, can_order=True, can_delete=True)
                choiceformset = ChoiceFormSet(request.POST, prefix='choice')

                if request.POST['submit_action'] == "Add Choice":
                     pass
                ...
        else:
                questionform = QuestionForm(prefix='question')
                ChoiceFormSet = formset_factory(ChoiceForm, extra=numchoices,
max_num=10, can_order=True, can_delete=True)
                choiceformset = ChoiceFormSet(prefix='choice')

        return render_to_response(
                                                          'create.html',
                                                          
{'questionform':questionform,
 
'choiceformset':choiceformset,
                                                          },
                                                          context_instance = 
RequestContext(request),
                                                          )


My questions are as follows:

1. How can I get a choice to be added if there is data present? I know
it has something to do with the management_form but I am unclear on
how to increment TOTAL-FORMS and INITIAL-FORMS in the management_form
or if I even need to. Anyhow, whenever I call ChoiceFormSet
(request.POST) a new blank choice is NEVER added, and if I leave out
request.POST, the extra choice form is added but all user input is
lost!

2. How are reordering and deleting choices processed? If I have a
submit action called, "Delete choices," what do I have to do in my
python code to actually get them off the form? Same with reordering.
Is this handled automatically?

3. Is there any way to get initial data in the ordering box on the
form? This would be done in the template, I imagine.

4. Are formsets the best way to do this or should I be doing
everything some other way? I am currently saving the Question and the
Choices at the same time--so the Question doesn't officially exist
until everything is saved at once.

I've been experimenting with this on and off for a while and cannot
find much good information on it. I highly appreciate any guidance!
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@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
-~----------~----~----~----~------~----~------~--~---

Reply via email to