0

I am making an RSVP form where the invitee can opt to bring a guest (or 2 or 3). The form has inputs generated x number of times, where x is the # of guests the user sets. So for example, if the user chooses "0" from the dropdown, they're not bringing a guest so the form only has inputs for 1 person (themselves). If they select "1" from the dropdown, the form duplicates all the fields, but there is still just one route/one submit button. The duplicated form fields are identical to the original, so the form has 2 inputs named "name", 2 inputs named "dinner", etc.

I get a dictionary when I print request.POST:

<QueryDict: {u'restrictions': [u'None', u'Gluten'], u'name': [u'Krista', u'Joel'], u'dinner': [u'chicken', u'fish'], u'csrfmiddlewaretoken': [u'iEjxo6KgLGHZWwvaxEd6I7CUd7GTs89VGpiKwmiCP9xqXKFK096s0vboK4pPQxea']}>

In vanilla python I can access the values like so:

dictionary['restrictions'][1] 

which returns "Gluten". But in Django this expression returns 'e' - the letter at index 1 of the key name 'restrictions'. Likewise,

for key, val1, val2 
   print key, val1, val2 

returns "too many values to unpack", presumably because it's unpacking the letters in the key name instead of the values.

How can I access the values? Alternatively, is there a better way to generate multiple forms based on the user's selection, with just one route and one submit button?

2
  • What's the output of request.POST['restrictions']? It should be a list, but it looks like it's a string for some reason. How are you accessing the dictionary, request.POST['restrictions'][1]? Commented Mar 21, 2017 at 22:02
  • @J.McBride the output of request.POST['restrictions'] is "Gluten", and request.POST['name'] is "Joel". So it's only returning the most recent value for each key. Yes, request.POST['restrictions'][0] returns 'r' and [1] returns 'e' - so it's returning the letter at those indexes for the word ['restrictions'] Commented Mar 21, 2017 at 22:20

1 Answer 1

2

You should try request.POST.getlist('restrictions')

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.