On Friday, February 21, 2014 6:14:14 PM UTC-4:30, Daniel Smith wrote:
>
> I'm using Django version 1.5.5. Here is a short snippet of the test I'm 
> writing:
>
> from django.test import TestCase
>
> class MyTest(TestCase):
>
>   def my_test(self):
>     url = ... location of my view ...
>     self.client.post(url, data={'active': False}, format='json')
>
>
> The problem I'm running into is that when I inspect request.POST inside my 
> view, I get: {'active': [u'False']}. The workaround I have right now is to 
> use json.loads(request.raw_post_data), but I'm wondering if this is a bug 
> or if I am just missing something. Any help would be greatly appreciated.
>
Hello,

The test client is not converting the data to json.

1. In your test module, you must convert the data to json and tell the 
correct content-type application/json:
json_data = json.dumps({'active': False})
data = self.client.post(path=url, data=json_data, 
content_type='application/json')

I notice that you use a format='json' parameter to post; I can't find 
anything about that parameter in the documentation or the code. You should 
use content_type='application/json' instead.

2. In the views module, you should parse the json data:
    if request.method == 'POST':
        body = request.body.decode('UTF-8')
        json_data = json.loads(body)
        print('views 12', json_data['active'])

Kindly note that you should be using request.body instead of the deprecated 
raw_post_data.

Regards,
Camilo

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/61dc3c96-6572-479a-b4c0-7717e071aed9%40googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to