python - Reading Checkbox in Django -


this not reading django checkbox. i've read through 7 different posts checkbox on here along 4 other pages in django documentation. situation bit different. can't read whether specific checkbox checked or not. value "true" both checkboxes when 1 checkbox checked. if nothing checked, worked expected. need use multiplechoicefield?

current output:

- john doe location true   ===> checked  - james smith location true    ===> unchecked 

ideally, list of dictionary contains

data [0] = {'john', 'doe', 1} data [1] = {'john', 'smith', 0} ... 

where '1' flag overwrite , '0' ignore.

background:

  1. user submits form
  2. second form displays previous information checkbox next names if duplicates found. if they're not duplicate, no checkbox appear.
  3. read in checkbox, process, , display final page.

checkbox entry

forms.py

from django import forms 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) 

views.py

def addname (request): ....  if request.method == 'post':     ....      if formset.is_valid ():         location = request.post ['site']         data = formset.cleaned_data          # store data session used in 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')          # 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.') 

response.html

<!doctype html> <html> <head lang="en">     <meta charset="utf-8">     {% load staticfiles %}     <link rel="stylesheet" type="text/css" href="{% static 'nameform/style.css' %}" >     <title>submitted entries</title> </head> <body>     <h1>submitted entries:</h1>     <h4>location: {{ location }}</h4>     <form action="process" method="post">{% csrf_token %}     <div id="tablefont">     <table id="table01">         <tr>             <th>first name</th>             <th>last name</th>             <th class="center">overwrite</th>         </tr>         {% info in data %}         <tr>             <td>{{ info.first_name }}</td>             <td>{{ info.last_name }}</td>             <td class="center"><input type="checkbox" name='overwrite-{{ forloop.counter0 }}'></td>  ===> testing checkbox                 <!--                 {% if info.overwrite %}                 <td class="center"><input type="checkbox" name='overwrite-{{ forloop.counter0 }}'></td>                 {% else %}                 <td class="center"></td>                 {% endif %}                 -->         </tr>         {% endfor %}     </table>     </div>     <br>     {% if errors %}     <p class="errorlh">error:         <ul>         {% error in errors %}             <li>{{ error }}</li>         {% endfor %}         </ul>     </p>     {% endif %}     <br>     <p><input type="submit" value="confirm">     <a href="{% url 'addname' %}">         <button type="button">cancel</button></a></p>     </form> </body> </html> 

success.html

<!doctype html> <html> <head lang="en">     <meta charset="utf-8">     <title>successfully added</title> </head> <body>     <h1>information captured:</h1>     <ul>     {% info in data %}         <li>{{ info.first_name }} {{ info.last_name }} {{ location }} {{ overwrite }}</li>     {% endfor %}     </ul>     <a href="{% url 'addname' %}">add more names</a> </body> </html> 

all checkbox inputs have same attribute name=overwrite so, when check one, 1 submitted in post overwrite=true.

you should give unique name each input. suggest use {{ forloop.counter0 }}, provide current iteration of loop, starting 0. index of current "info" in "data".

{% info in data %}     <tr>         <td>{{ info.first_name }}</td>         <td>{{ info.last_name }}</td>         <td class="center"><input type="checkbox" name='overwrite-{{ forloop.counter0 }}'></td>  ===> testing checkbox             <!--             {% if info.overwrite %}             <td class="center"><input type="checkbox" name='overwrite' value="1"></td>             {% else %}             <td class="center"></td>             {% endif %}             -->     </tr> {% endfor %} 

this way, can match form field in post "info" in backend.


Comments

Popular posts from this blog

shopping cart - Page redirect not working PHP -

php - How to modify a menu to show sub-menus -

python - Installing PyDev in eclipse is failed -