Reading Dynamic Checkbox in Django

2015-04-28 Thread Ken Nguyen
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':

checkbox in Django

2014-07-28 Thread dharmi patel
Hi,

As I am a new for Django and I use Django for making web application.
My problem is:
I have product list and i have to select more than one product. once i have 
select the products. I have to compare those selected product but there are 
few criteria of comparison like compared by name , version ,release.
notes: product has number of packages and package has package name, 
version, release,.
In short the logic I have to implement is,  if i select 2 product and i 
will compare these 2 product by package version then they should display 
the all common packages of both product which have same packages with same 
version.


Can any one help me , how can i implement this logic?

-- 
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 django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
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/4c49-0c37-46f5-8bd8-41988a2b6fc3%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.