Reputation: 5402
django.utils.datastructures.MultiValueDictKeyError: "'user_data'"
Is what i get when trying to access user_data from the request.POST
post_data = dict(request.POST)
print(post_items)
returns
{'user_data[first_name]': ['Jamie'], 'user_data[name_last]': ['Lannister'], 'campus': ['McHale Hall'], 'user_data[twitter]': ['@jamielan']}
So if I try to get just the user_data, I try this (doesn't work)
post_data = dict(request.POST)
user_data = post_data['user_data']
I just want to get all instances of user_data in this dict and store as json. How can I do that?
Expected out put would be something like
Upvotes: 0
Views: 4023
Reputation: 25549
Your POST
data is really weird but for the sake of correctness, you should do:
first_name = post_data["user_data[first_name]"]
name_last = post_data["user_data[name_last]"]
Because the string user_data[first_name]
is the key for the dict not just string user_data
.
Edit:
If you want to convert user data into dict, you should loop on request.POST
and check for keys that contains user_data
keyword:
user_data = {}
for key, value in request.POST.iteritems():
if 'user_data' in key:
field = key.split('[')[1].replace(']', '')
user_data[key] = value
# convert into json
json_user_data = json.dumps(user_data)
Upvotes: 1