0

Im trying to send my serialized form data to django where it will should be stored in "mylist" as a list "[video, audio]". views.py return 0 everytime no matter what boxes I check on my form.

html form

<form id="myform" method="POST">
<input type="checkbox" name="list" value="audio"/> Audio<br />
<input type="checkbox" name="list" value="video"/> Video<br />
<input type="submit" value="Get Custom Library!" /> 
</form>

jquery ajax

<script language="JavaScript">
$(document).ready(function() {
$("#myform").submit(function() {

        $.ajax({
        type: "POST",
        url: 'django/builder/buildit',
        data: $(this).serialize(),
        success: function(response){
            alert(response);
        }
    });
    return false;
});
});
</script>

views.py

from django.http import HttpResponse
def main(request):
  mylist = request.POST.getlist('list')
  message = 0
  for item in mylist:
    if item == 'video':
      message = 'vid'
    elif item == 'audio':
      message = 'aud'
  return HttpResponse(message)
6
  • data: { list: myCheckboxes }, Jun 13, 2012 at 20:30
  • Try inserting print(item) in views.py to find out its actual value?
    – Junuxx
    Jun 13, 2012 at 22:53
  • 1
    Why don't you use if 'video' in mylist instead of for loop?
    – VisioN
    Jun 13, 2012 at 22:53
  • I changed my views.py and removed my for loop. I am getting a internal server error on my views page now which means "mylist" is not a list. Why isnt my serialized data being added as a list to "mylist"? Jun 13, 2012 at 23:03
  • print POST variables and see their structure you can print request.POST in view or drop in a debugger so you can see what's going on.
    – dm03514
    Jun 13, 2012 at 23:04

1 Answer 1

1

You're not giving a key value pair to the data argument, but you shouldn't even have to go that far - you don't have to gather the checkbox values yourself. You can replace this:

    var myCheckboxes = new Array();
    $("input:checked").each(function() {
       myCheckboxes.push($(this).val());
    });

    $.ajax({
        type: "POST",
        url: 'django/builder/buildit',
        data: myCheckboxes,
        success: function(response){
            alert(response);
        }
    });

With this, using serialize:

    $.ajax({
        type: "POST",
        url: 'django/builder/buildit',
        data: $('#myform').serialize(),
        success: function(response){
            alert(response);
        }
    });
5
  • How would I store the serialized data in the "mylist" variable as a list in django that looks like this [video, audio] Jun 13, 2012 at 20:30
  • @user1442957 What you have in your view should work with the above code. Jun 13, 2012 at 20:33
  • I edited my main post and now views.py just keeps returning '0' no matter what is checked. I dont think my data is getting stored in "mylist" as a list. Jun 13, 2012 at 20:49
  • 1. Please don't edit your question in response to an answer to change what's being asked. 2. What happens when you log/print the contents of mylist within your view? Jun 13, 2012 at 21:09
  • I have debugging enabled but how do I print or log the value of a variable? Jun 13, 2012 at 21:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.