Hi Folks,

First time posting on here so if I violate any laws, please forgive me.  I 
also posted the same question 
<http://stackoverflow.com/questions/29858678/reading-checkbox-in-django>on 
StackOverflow but didn't get any answers so I'm hoping this place will get 
more attention.

I'm having a hard time reading whether the checkbox is checked or not.  I 
created a formset with just the first_name and last_name since my initial 
page is only asking for those 2 information.  Once entered and validated, 
the next page will show a checkbox next to the duplicate names.

Here are the things that I've tried:

   - Changed the checkbox's name to iterate with the for-loop for 
   uniqueness.  (Regardless if I check the box or not, the results are always 
   the same {'overwrite': False}.)
   - Added the Boolean value with the first_name & last_name formset 
   (Result - 'ManagementForm data is missing or has been tampered with')

I think the problem lies with obtaining the 'POST' data.  Any help is much 
appreciated.


*Step 1*

<https://lh3.googleusercontent.com/-1r83DW4K2_k/VT8tl4R-ZFI/AAAAAAAAJHk/UrAnlXHTMJk/s1600/Ubuntu%2B64-bit-2015-04-27-23-48-41.png>


*Step 2*

<http://i.stack.imgur.com/UHGCS.jpg>

*Step 3* *- Output Expectation*

John Doe Location A 1 ("1" is checked)

James Smith Location A 0 ("0" is not checked)


*forms.py*

from django import forms
from django.forms.formsets import BaseFormSet


class UserInfo (forms.Form):

    first_name = forms.CharField (max_length = 20, required = False)
    last_name = forms.CharField (max_length = 20, required = False)
    overwrite = forms.BooleanField (required = False)


class BaseUserInfoFormSet (BaseFormSet):

    def clean (self):
        if any (self.errors):
            return

        firstnames = []
        lastnames = []
        errors = []

        for form in self.forms:
            firstname = form.cleaned_data.get ('first_name')
            lastname = form.cleaned_data.get ('last_name')

            if ((firstname in firstnames) or (lastname in lastnames)) and 
len (errors) < 2:
                errors.append ('First and/or last name must be unique')
            if ((firstname == '') or (lastname == '')) and len (errors) < 2:
                errors.append ('First and/or last name cannot be blanked')

            firstnames.append (firstname)
            lastnames.append (lastname)

        if errors:
            raise forms.ValidationError (errors)

        return self.cleaned_data


#class DuplicateForm (forms.Form):
#    overwrite = forms.BooleanField (required = False)

*views.py*

from django.shortcuts import render, render_to_response
from django.forms.formsets import formset_factory
from userinfo.forms import UserInfo
from userinfo.forms import BaseUserInfoFormSet
from userinfo.addName import webform


# Create your views here.
def addname (request):
    UserInfoSet = formset_factory (UserInfo, formset = BaseUserInfoFormSet, 
extra = 2, max_num = 3)
    if request.method == 'POST':
        formset = UserInfoSet (request.POST)

        if formset.is_valid ():
            location = request.POST ['site']
            names = formset.cleaned_data

            request.session ['location'] = location
            request.session ['names'] = names

            for name in names:
                firstname = name.get ('first_name')
                lastname = name.get ('last_name')

                if firstname and lastname:
                    webform (firstname, lastname, location)

            context = {'names': names, 'location': location}
            return render (request, 'userinfo/response.html', context)

    else:
        formset = UserInfoSet ()

    context = {
        'formset': formset,
        'first_name_0': request.POST.get ('form-0-first_name', ''),
        'last_name_0': request.POST.get ('form-0-last_name', ''),
        'first_name_1': request.POST.get ('form-1-first_name', ''),
        'last_name_1': request.POST.get ('form-1-last_name', ''),
    }

    return render (request, 'userinfo/addname.html', context)


def response (request):
    location = request.session ['location']
    names = request.session ['names']

    UserInfoSet = formset_factory (UserInfo, formset = BaseUserInfoFormSet, 
extra = 2, max_num = 3)
    if request.method == 'POST':
        formset = UserInfoSet (request.POST)

        if formset.is_valid ():
            data = formset.cleaned_data
            for duplicate in data:
                overwrite = duplicate.get ('overwrite')

    #if request.method == 'POST':
    #    form = DuplicateForm (request.POST)

    #    if form.is_valid ():
    #        data = form.cleaned_data

            #for duplicate in data:
            #    overwrite = duplicate.get ('overwrite')


            context = {'names': names, 'location': location, 'data': data}
            return render (request, 'userinfo/results.html', context)

    return render_to_response ('No valid data.')


*addname.html*

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title>Add Name</title>
</head>
<body>
    <h1>Add Name</h1>
    <form action="" method="POST">{% csrf_token %}
    <h3>Select a site</h3>
    <div class="radioSection">
        <p><input type="radio" id="radio1" name="site" value="Location A" 
checked>
        <label for="radio1">Location A</label>
        <input type="radio" id="radio2" name="site"value="Location B">
        <label for="radio2">Location B</label></p>
     </div>
        {{ wizard.management_form }}
        {{ formset.management_form }}
        <label for="id_form-0-first_name">First Name:</label>
        <input id="id_form-0-first_name" maxlength="20" name=
"form-0-first_name" type="text"
               value="{{ first_name_0 }}" required />
        <label for="id_form-0-last_name">Last Name:</label>
        <input id="id_form-0-last_name" maxlength="20" name=
"form-0-last_name" type="text"
               value="{{ last_name_0 }}" required />
        <p><label for="id_form-1-first_name">First Name:</label>
        <input id="id_form-1-first_name" maxlength="20" name=
"form-1-first_name" type="text"
               value="{{ first_name_1 }}" />
        <label for="id_form-1-last_name">Last Name:</label>
        <input id="id_form-1-last_name" maxlength="20" name=
"form-1-last_name" type="text"
               value="{{ last_name_1 }}" /></p>
    <p><input type="submit" value="Add Name" /></p>
    </form>

    {% if formset.errors %}
        <b>Please correct the error below:</b>
        <ul>
        {% for error in formset.non_form_errors %}
            <li style="color: red;">{{ error }}</li>
        {% endfor %}
        </ul>
    {% endif %}
</body>
</html>

*response.html*

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    {% load staticfiles %}
    <link rel="stylesheet" type="text/css" href="{% static 
'userinfo/style.css' %}" >
    <title>Confirmation</title>
</head>
<body>
    <h2>Submitted Entries:</h2>
    <form action="response" method="POST">{% csrf_token %}
    {{ wizard.management_form }}
      <table>
      <tr>
        <th>Firstname</th>
        <th>Lastname</th>
        <th class="center">Location</th>
        <th class="center">Overwrite</th>
      </tr>
      {% for name in names %}
          <tr>
            <td>{{ name.first_name }}</td>
            <td>{{ name.last_name }}</td>
            <td class="center">{{ location }}</td>
            <td class="center" ><input type="checkbox" name="overwrite-{{ 
forloop.counter0 }}"></td>
          </tr>
      {% endfor %}
    </table>
    <p><input type="submit" value="Confirm">
    <a href="{% url "addname" %}">
        <button type="button">Cancel</button>
    </a></p>
    </form>
</body>
</html>

*results.html*

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title>Results</title>
</head>
<body>
    <h2>Submitted Entries:</h2>
    <ul>
        {% for name in names %}
            <li>{{ name.first_name }} {{ name.last_name }} {{ location }} 
{{ data }}</li>
        {% endfor %}
    </ul>
    <p><a href="{% url "addname" %}">Add more names</a></p>
</body>
</html>


-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/8c9bf35a-5ec0-4d15-85ea-3b118e694088%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to