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/JHk/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':