fh_bash
fh_bash

Reputation: 1795

Django QueryDict variable, passed via application-form-post, how to get variables?

I trying to get some variables from one server, this server isn't mine, and I receive the POST like these:

<QueryDict: {"'data[id]': ['83A0C50B5A0A43AD8F60C1066B16A163'], 'data[status]': ['paid'], 'event': ['invoice.status_changed']": ['']}>

Here is the code:

def get_iugu_retorno(request):
    d1 = request.POST
    d2 = d1.get['data[id]']

I need to get data[id], data[status] and event... but Django appears to get all these information like a flattern string instead a Dict.

How is the best way to solve these?

I also try to create a list:

d2 = d1.getlist('data')

and nothing...

I`m using Django 1.8

Upvotes: 1

Views: 746

Answers (2)

Daniel Roseman
Daniel Roseman

Reputation: 599906

This isn't form-encoded data at all, so you can't access it as if it is. It appears to be a form of JSON. So you need to access the post body directly.

d1 = json.loads(request.body)
d2 = d1.get('data[id]')

Upvotes: 2

Simon
Simon

Reputation: 3727

You are right in the assumption that data[id] is the name of the key, and not a list, but you are simply accessing the QueryDict the wrong way (get[], it's get()). The following code works fine:

from django.http import QueryDict
qd = QueryDict('data[id]=83A0C50B5A0A43AD8F60C1066B16A163&data[status]=paid&event=invoice.status_changed=')
qd.get('data[id]')
>>> u'83A0C50B5A0A43AD8F60C1066B16A163'

Upvotes: 1

Related Questions