Below is the code I am using in my html to send an array of userids(numbers). On click of the checkboxes I am sending the array (checkIds):-
var checkIds = []
$(document).on("click","#group_save",function(){
$("#candidate-data tr").each(function(index,rowhtml){
var checked= $('input[id="groups"]:checked',rowhtml).length;
checkIds = jQuery.unique(checkIds)
if (checked==1){
checkIds.push($('.hideMe',rowhtml).text());
}
});
alert(checkIds);
var jsonText = JSON.stringify(checkIds)
checkIds.length = 0;
var groupName = $('input:text[name="group_name"]').val();
alert(groupName)
$.ajax({
url: "{% url 'userinfo:groups' %}" + "?gname="+groupName,
type: "POST",
data:jsonText,
dataType: 'json',
success: function(){
notyfy({type: "success", layout: "topCenter", text: "Saved", timeout: 5000});
}
});
});
How do I access the data:jsonText
in my views. py I am doing this way which does not work I have to save the gname
(name) along with the array(jsonText
) ids in two tables Groups and GroupMembers in groups table i have to save group name(gname) and after saving i have to get the id of the saved group object and along with the array of userids(jsonText) have to be saved in GroupMembers table:-
def groups(request):
gname = request.GET.get('gname', None)
if request.method == 'POST':
Groups(name=gname).save()
usersV = request.POST.get('jsonText')
x = request.GET.get('id',None)
print x
if x != "0":
for users in usersV:
print users
GroupMembers(group_id=x,user_id=users).save()
return HttpResponse("Success")
else:
return HttpResponse("Error")