Thank you very much for the help. So it sounds like I just have to suck it
up and call json.loads() on request.body.
The 'format=json' thing is related to also using the TastyPie test client.
I've tried both and forgot where that parameter was used.
Thanks,
Daniel
On Saturday, February 22, 2014 10:56:21 AM UTC-5, Camilo Torres wrote:
>
> 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 [email protected].
To post to this group, send email to [email protected].
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/3ec8854d-f40b-4cc6-94bc-dc6d927cf60a%40googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.