Note that there are some explanatory texts on larger screens.

plurals
  1. PODjango's Querydict bizarre behavior: bunches POST dictionary into a single key
    text
    copied!<p>I'm experiencing a really bizarre behavior when using the test client in django.</p> <p>I'm using a <code>POST</code> to send data to my django app. I usually do this from an iPhone app and/or a test html form. On the server side, this is how I handle it:</p> <pre><code>def handle_query(request): print request q = con.QueryLog() q.ID = request.POST.get('ID', '') q.device = request.POST.get('device-model', '') .... </code></pre> <p>That print statement looks like what you would expect, i.e. each parameter in the post request is turned into a key in the dictionary:</p> <p>POST: QueryDict: {u'app-version': [u'3.0'], u'server-version': [u'v3d0'],</p> <p>However, I started writing some tests using Django's test client, and no matter what I try, the dictionary of POST parameters I send in the post request get bunched into a single key in the <code>QueryDict</code>. Allow me to illustrate with some code:</p> <p>class SearchTest(TestCase): def setUp(self): pass</p> <pre><code>def test_search(self): request = HttpRequest() data = '{"amzn_locale": "com"}' # request._raw_post_data = data resp = self.client.post( '/is/', data=data, content_type='application/x-www-form-urlencoded', # content_type='application/json', ) </code></pre> <p>The same print statement on the server side shows the inexplicable grouping of the dictionary into a string:</p> <pre><code>POST: QueryDict: {u'{"amzn_locale":"com"}': [u'']}&gt;, </code></pre> <p>If I set data to an actual dictionary, same thing</p> <pre><code>data = {"amzn_locale": "com"} </code></pre> <p>Setting the request._raw_post_data doesn't change anything. Nor does changing</p> <pre><code>content_type='application/json' </code></pre> <p>Any help would be much appreciated. From this stackoverflow question it seems like I'm not the first one to run into this <a href="https://stackoverflow.com/questions/2579235/iphone-json-post-request-to-django-server-creates-querydict-within-querydict">iphone Json POST request to Django server creates QueryDict within QueryDict</a></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload