Skip to content Skip to sidebar Skip to footer

Django - Getting Post To Return A Dictionary Where The Values Are Lists

been stuck on this one for a while: We've got a view that has a number of dishes on it, like this: spaghetti tacos burger we want to have the user be able to enter two pieces of i

Solution 1:

Unfortunately, you're just going to have to use a different id for each grade and comment, then do some server side parsing to format your data nicely. Either that or you can do client side parsing and use a $.post to send JSON data to the server. I'd lean towards the second option, honestly, although you'll still have to do some server side validation/data sanitization.

If you go for the second option, you could do something like this:

<divid='spaghetti'><inputfield-type='grade' /><inputfield-type='comment' /></div><script>
  $(document).ready(function() {
    $('input[type=submit]').click(function() {
      var data = {}
      var grade = $('#spaghetti').children('input[field-type=grade]').first().val();
      var comment = $('#spaghetti').children('input[field-type=comment]').first().val();
      data['spaghetti'] = [grade, comment];
      $.post('url', data);
    }
  }

Solution 2:

In the form, name one spaghetti_grade and another spaghetti_taste. Then you can access them using:

request.POST.get('spaghetti_grade','') and request.POST.get('spaghetti_taste','')

that will allow you to get both without the need for a list. You can then do the same for the other foods.

Post a Comment for "Django - Getting Post To Return A Dictionary Where The Values Are Lists"