title: Reading Checkbox in Django
body: <p>This is <strong>NOT</strong> just another Reading Django Checkbox.  I've read through 7 different posts about Checkbox on here along with 4 other pages in Django Documentation.  My situation is a bit different.  I can't read whether a specific checkbox is checked or not.  I just get a value "True" for both checkboxes even when only one checkbox is checked.  If nothing is checked, it worked as expected.  Do I really need to use the MultipleChoiceField?</p>
  
  <p><strong>Current Output:</strong></p>
  
  <pre><code>- John Doe Location A True   ===&gt; Checked
   - James Smith Location A True    ===&gt; Unchecked
  </code></pre>
  
  <p>Ideally, I would like a list of dictionary that contains</p>
  
  <pre><code>data [0] = {'John', 'Doe', 1}
  data [1] = {'John', 'Smith', 0}
  ...
  </code></pre>
  
  <p>where '1' is the flag for overwrite and '0' is to ignore.</p>
  
  <p><strong>Background:</strong></p>
  
  <ol>
  <li>User submits a form</li>
  <li>Second form displays the previous information with checkbox next to the names if duplicates are found.  If they're not duplicate, no checkbox will appear.</li>
  <li>Read in the checkbox, process, and display the final page.</li>
  </ol>
  
  <p><img src="https://i.stack.imgur.com/UHGCS.jpg" alt="Checkbox Entry"></p>
  
  <p>forms.py</p>
  
  <pre><code>from django import forms
  from django.forms.formsets import BaseFormSet
  
  class NameForm (forms.Form):
  
      first_name = forms.CharField (max_length = 20, required = False)
      last_name = forms.CharField (max_length = 20, required = False)
  
  class BaseNameFormSet (BaseFormSet):
  ...
  
  class CheckBox (forms.Form):
      overwrite = forms.BooleanField (required = False)
  </code></pre>
  
  <p>views.py</p>
  
  <pre><code>def addname (request):
  ....
  
  if request.method == 'POST':
      ....
  
      if formset.is_valid ():
          location = request.POST ['site']
          data = formset.cleaned_data
  
          # Store data into session to be used in another method
          request.session ['location'] = location
          request.session ['data'] = data
  
  
  def process (request):
  
      location = request.session ['location']
      data = request.session ['data']
  
      if request.method == 'POST':
          form = CheckBox (request.POST)
  
          if form.is_valid ():
              overwrite = form.cleaned_data.get ('overwrite')
  
          # for duplicate in checkboxes:
          #    overwrite = duplicate.get ('overwrite')
          print (overwrite)
  
          context = {'data': data, 'location': location, 'overwrite': overwrite}
          return render (request, 'nodeform/success.html', context)
  
      return HttpResponse ('No Overwrite Data.')
  </code></pre>
  
  <p>response.html</p>
  
  <pre><code>&lt;!DOCTYPE html&gt;
  &lt;html&gt;
  &lt;head lang="en"&gt;
      &lt;meta charset="UTF-8"&gt;
      {% load staticfiles %}
      &lt;link rel="stylesheet" type="text/css" href="{% static 'nameform/style.css' %}" &gt;
      &lt;title&gt;Submitted Entries&lt;/title&gt;
  &lt;/head&gt;
  &lt;body&gt;
      &lt;h1&gt;Submitted Entries:&lt;/h1&gt;
      &lt;h4&gt;Location: {{ location }}&lt;/h4&gt;
      &lt;form action="process" method="POST"&gt;{% csrf_token %}
      &lt;div id="tablefont"&gt;
      &lt;table id="table01"&gt;
          &lt;tr&gt;
              &lt;th&gt;First Name&lt;/th&gt;
              &lt;th&gt;Last Name&lt;/th&gt;
              &lt;th class="center"&gt;Overwrite&lt;/th&gt;
          &lt;/tr&gt;
          {% for info in data %}
          &lt;tr&gt;
              &lt;td&gt;{{ info.first_name }}&lt;/td&gt;
              &lt;td&gt;{{ info.last_name }}&lt;/td&gt;
              &lt;td class="center"&gt;&lt;input type="checkbox" name='overwrite-{{ forloop.counter0 }}'&gt;&lt;/td&gt;  ===&gt; Testing Checkbox
                  &lt;!--
                  {% if info.overwrite %}
                  &lt;td class="center"&gt;&lt;input type="checkbox" name='overwrite-{{ forloop.counter0 }}'&gt;&lt;/td&gt;
                  {% else %}
                  &lt;td class="center"&gt;&lt;/td&gt;
                  {% endif %}
                  --&gt;
          &lt;/tr&gt;
          {% endfor %}
      &lt;/table&gt;
      &lt;/div&gt;
      &lt;br&gt;
      {% if errors %}
      &lt;p class="errorlh"&gt;Error:
          &lt;ul&gt;
          {% for error in errors %}
              &lt;li&gt;{{ error }}&lt;/li&gt;
          {% endfor %}
          &lt;/ul&gt;
      &lt;/p&gt;
      {% endif %}
      &lt;br&gt;
      &lt;p&gt;&lt;input type="submit" value="Confirm"&gt;
      &lt;a href="{% url 'addname' %}"&gt;
          &lt;button type="button"&gt;Cancel&lt;/button&gt;&lt;/a&gt;&lt;/p&gt;
      &lt;/form&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>success.html</p>
  
  <pre><code>&lt;!DOCTYPE html&gt;
  &lt;html&gt;
  &lt;head lang="en"&gt;
      &lt;meta charset="UTF-8"&gt;
      &lt;title&gt;Successfully Added&lt;/title&gt;
  &lt;/head&gt;
  &lt;body&gt;
      &lt;h1&gt;Information captured:&lt;/h1&gt;
      &lt;ul&gt;
      {% for info in data %}
          &lt;li&gt;{{ info.first_name }} {{ info.last_name }} {{ location }} {{ overwrite }}&lt;/li&gt;
      {% endfor %}
      &lt;/ul&gt;
      &lt;a href="{% url 'addname' %}"&gt;Add more names&lt;/a&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>