How to solve MultiValueDictKeyError

2,425 views
Skip to first unread message

Kayode Odeyemi

unread,
Sep 21, 2011, 6:33:23 AM9/21/11
to django-users, django-d...@googlegroups.com
Hello friends,

I don't know why Django is so unstable.

Before restarting my computer everything works fine. Django could parse a simple
POST request without complaining KeyError.

I'm simply making a POST request like this:

curl -v -H "Content-Type: application/json" -A 'Mozilla' -X POST --data 'fees={"fees":{"status":"pending","timeout":5}, "hostel":{"status":"pending","timeout": 3}}' http://127.0.0.1:8000/api/transaction/add/ > post_data.txt

I also have it in valid dict format like this:

curl -v -H "Content-Type: application/json" -X POST --data 'fees={"fees":[("status","pending"),("timeout",5)], "hostel":[("status","pending"),("timeout", 3)]' http://127.0.0.1:8000/api/transaction/add/ > post_data.txt

This worked throughout yesterday. Just hoping to continue from where I left off, and I can't progress as I would love to.

Trace:

Environment:


Request Method: POST
Request URL: http://127.0.0.1:8000/api/transaction/add/

Django Version: 1.3
Python Version: 2.7.1
Installed Applications:
['django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.sites',
 'django.contrib.messages',
 'django.contrib.admin',
 'django.contrib.humanize',
 'axes',
 'webapp',
 'djangorestframework',
 'django_tables',
 'django_tables.app',
 'pagination',
 'south',
 'djcelery']
Installed Middleware:
('django.middleware.common.CommonMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'djangoflash.middleware.FlashMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'axes.middleware.FailedLoginMiddleware',
 'audit_log.middleware.UserLoggingMiddleware')


Traceback:
File "C:\Python27\lib\site-packages\django-1.3-py2.7.egg\django\core\handlers\base.py" in get_response
  111.                         response = callback(request, *callback_args, **callback_kwargs)
File "C:\wamp\www\edupay\api\views.py" in add_transaction
  44.     req_fees = json.loads(QueryDict(request.POST.__getitem__('fees')))
File "C:\Python27\lib\site-packages\django-1.3-py2.7.egg\django\utils\datastructures.py" in __getitem__
  256.             raise MultiValueDictKeyError("Key %r not found in %r" % (key, self))

Exception Type: MultiValueDictKeyError at /api/transaction/add/
Exception Value: 'Key \'fees\' not found in <QueryDict: {u\'identifier\': [u\' \'], u\'amount\': [u\'0.0\'], u\'code\': [u\'UNIA\'], u\' fees\': [u\'{"fees":[("status","pending"),("timeout",5)], "hostel":[("status","pending"),("timeout", 3)]\'], u\'digest\': [u\'5D316CD2311678A1B12F6152988F3097\']}>'

The problem is simply understandable, but why didn't Django say this when I worked on it yesterday. This is not fair Djanjo. It is not!

Somebody please help me.

Thanks

--
Odeyemi 'Kayode O.
http://www.sinati.com. t: @charyorde

Tom Evans

unread,
Sep 21, 2011, 6:43:18 AM9/21/11
to django...@googlegroups.com
On Wed, Sep 21, 2011 at 11:33 AM, Kayode Odeyemi <dre...@gmail.com> wrote:
> Hello friends,
>
> I don't know why Django is so unstable.

I don't know why you cross posted this to django-developers.

>
> Before restarting my computer everything works fine. Django could parse a
> simple
> POST request without complaining KeyError.
>
> I'm simply making a POST request like this:
>
> curl -v -H "Content-Type: application/json" -A 'Mozilla' -X POST --data
> 'fees={"fees":{"status":"pending","timeout":5},
> "hostel":{"status":"pending","timeout": 3}}'
> http://127.0.0.1:8000/api/transaction/add/ > post_data.txt

Thats not x-www-form-urlencoded data, so expecting django to parse it
is not going to happen. Django parses x-www-form-urlencoded data.

>
> I also have it in valid dict format like this:
>
> curl -v -H "Content-Type: application/json" -X POST --data
> 'fees={"fees":[("status","pending"),("timeout",5)],
> "hostel":[("status","pending"),("timeout", 3)]'
> http://127.0.0.1:8000/api/transaction/add/ > post_data.txt
>
> This worked throughout yesterday. Just hoping to continue from where I left
> off, and I can't progress as I would love to.
>

It didn't work yesterday, you are mistaken. Django has never
automatically parsed json encoded post data, so if you think it did
yesterday, you are wrong.

Tom

Kayode Odeyemi

unread,
Sep 21, 2011, 6:52:54 AM9/21/11
to django...@googlegroups.com
On Wed, Sep 21, 2011 at 11:43 AM, Tom Evans <teva...@googlemail.com> wrote:
On Wed, Sep 21, 2011 at 11:33 AM, Kayode Odeyemi <dre...@gmail.com> wrote:
> Hello friends,
>
> I don't know why Django is so unstable.

I don't know why you cross posted this to django-developers.

My apologies for cross-posting. (My hands to my chest and head bowed)

>
> Before restarting my computer everything works fine. Django could parse a
> simple
> POST request without complaining KeyError.
>
> I'm simply making a POST request like this:
>
> curl -v -H "Content-Type: application/json" -A 'Mozilla' -X POST --data
> 'fees={"fees":{"status":"pending","timeout":5},
> "hostel":{"status":"pending","timeout": 3}}'
> http://127.0.0.1:8000/api/transaction/add/ > post_data.txt

Thats not x-www-form-urlencoded data, so expecting django to parse it
is not going to happen. Django parses x-www-form-urlencoded data.

OK! Thanks


> This worked throughout yesterday. Just hoping to continue from where I left
> off, and I can't progress as I would love to.
>

It didn't work yesterday, you are mistaken. Django has never
automatically parsed json encoded post data, so if you think it did
yesterday, you are wrong.

I can swear it worked. I had the data in a queue and then processed. Seriously.
I'm not kidding.

Kayode Odeyemi

unread,
Sep 21, 2011, 7:28:42 AM9/21/11
to django...@googlegroups.com
On Wed, Sep 21, 2011 at 11:43 AM, Tom Evans <teva...@googlemail.com> wrote:

On Wed, Sep 21, 2011 at 11:33 AM, Kayode Odeyemi <dre...@gmail.com> wrote:
> curl -v -H "Content-Type: application/json" -A 'Mozilla' -X POST --data
> 'fees={"fees":{"status":"pending","timeout":5},
> "hostel":{"status":"pending","timeout": 3}}'
> http://127.0.0.1:8000/api/transaction/add/ > post_data.txt

Thats not x-www-form-urlencoded data, so expecting django to parse it
is not going to happen. Django parses x-www-form-urlencoded data.

The problem is not the content-type. It's because of the space in between --data and 'fees=

I've fixed that and it worked :)

Thanks anyway

It didn't work yesterday, you are mistaken. Django has never
automatically parsed json encoded post data, so if you think it did
yesterday, you are wrong.

It worked yesterday and worked today. I hope it works tomorrow :)

Florian Apolloner

unread,
Sep 21, 2011, 7:36:40 AM9/21/11
to django-d...@googlegroups.com, django-users
Hi,

please post in django-users, this mailinglist is about the development of django itself, not about enduser problems.

Jacob Kaplan-Moss

unread,
Sep 21, 2011, 1:38:36 PM9/21/11
to django...@googlegroups.com
On Wed, Sep 21, 2011 at 5:43 AM, Tom Evans <teva...@googlemail.com> wrote:
> On Wed, Sep 21, 2011 at 11:33 AM, Kayode Odeyemi <dre...@gmail.com> wrote:
>> Hello friends,
>>
>> I don't know why Django is so unstable.
>
> I don't know why you cross posted this to django-developers.

Tom, the snarky tone is un-called-for and not OK. Cross-posting is
against our normal standards, but there's not reason for Kayode to
have known that. Please try to be nice when telling people about our
community standards.

And Kayode: I think you'll find you have a lot more luck getting your
questions answered if you're polite and don't insult the work of a
bunch of volunteers. Calling Django "unstable" is a really bad way to
motivate the people who wrote it to try to help you out.

Remember folks: at the other end of that email is "a real person, a
lot like you" (http://sivers.org/real).

Jacob

Reply all
Reply to author
Forward
0 new messages