vendredi 24 avril 2015

Reading Checkbox in Django

This is NOT 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?

Current Output:

- John Doe Location A True   ===> Checked
 - James Smith Location A True    ===> Unchecked

Ideally, I would like a list of dictionary that contains

data = {'John', 'Doe', 1}
data = {'John', 'Smith', 0}
...

where '1' is the flag for overwrite and '0' is to ignore.

Background:

  1. User submits a form
  2. 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.
  3. Read in the checkbox, process, and display the final page.

Checkbox Entry

forms.py

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)

views.py

def addnode (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.')

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>
        {% for info in data %}
        <tr>
            <td>{{ info.first_name }}</td>
            <td>{{ info.last_name }}</td>
            <td class="center"><input type="checkbox" name='overwrite'></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 %}
    </table>
    </div>
    <br>
    {% if errors %}
    <p class="errorlh">Error:
        <ul>
        {% for 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>
    {% for 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>




Aucun commentaire:

Enregistrer un commentaire