3

I was exploring the mechanism of formsets in Django. Here is a gist of what I did:

  1. Created a sample choicefield form

forms.py

      from django import forms

      MONTH_CHOICES = (("JANUARY", "January"),
                       ("FEBRUARY", "February"),
                       ("MARCH", "March"),
                       ("DECEMBER", "December"),)

      class NameForm(forms.Form):
            your_name = forms.ChoiceField(label="Your Name",choices=MONTH_CHOICES)
  1. Added Views Code with a formset instance for the NameForm and data handling.

views.py

    from django.shortcuts import render
    from django.http import HttpResponseRedirect,HttpResponse
    from django.forms.formsets import formset_factory
    from .forms import NameForm

    def index(request):
        lfs = formset_factory(NameForm)
        if request.method == 'POST':
            postedformset = lfs(request.POST)
            if postedformset.is_valid():
                for formz in postedformset:
                    print formz.cleaned_data['your_name']
            return HttpResponseRedirect('/polls/thanks/')
        else:
            form = NameForm()
                return render(request,'name.html',{'form':lfs})

    def thanks(request):
        return HttpResponse('Form filled. Thanks!')

3.A HTML for implementing the formset with a bit of javascript for adding and deleting forms

name.html

<html>
<head>
    <title>DJANGO - First Forms</title>
</head>
<body>
    <form action="/polls/" method="POST">
        {{ form.management_form }}      
        <div class="link-formset">
            {{ form }}
        </div>
        <input type="Submit"/>
    </form>
</body>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery.formset/1.2.2/jquery.formset.min.js"></script>
<script>
    $('.link-formset').formset({
        addText: 'add link',
        deleteText: 'remove'
    });
</script>

Implementation was done using this using this reference

Question

I was able to add multiple choicefields and save the form without any issues. However, only the choice of the first form was printed when

     print formz.cleaned_data['your_name')

was executed. Querydict from request.POST has choices from all forms in formset (refer Update)

Please share your thoughts on this.

PS : I did check on questions like this and this, but still not able to trace the root cause.

Update

For 2 forms in my formset, when I tried to print request.POST, below QueryDict was obtained which has information from both forms..

<QueryDict: {u'form-1-form-TOTAL_FORMS': [u''],  u'form-1-form-MIN_NUM_FORMS': [u''], u'form-1-form-MAX_NUM_FORMS': [u''],   u'form-1-form-INITIAL_FORMS': [u''], u'form-MAX_NUM_FORMS': [u'1000', u'1000'],   u'form-1-your_name': [u'FEBRUARY'], u'form-0-your_name': [u'FEBRUARY'],   u'form-MIN_NUM_FORMS': [u'0', u'0'], u'form-INITIAL_FORMS': [u'0', u'0'],   u'form-TOTAL_FORMS': [u'2', u'1']}>   
Community
  • 1
  • 1
data_set
  • 339
  • 2
  • 15

0 Answers0