request.POST parsing without form

1,946 views
Skip to first unread message

taso

unread,
Jan 28, 2011, 2:05:21 PM1/28/11
to Django users
Hi,

I am using django strictly as a backend for data processing. As such I
am not using any forms or templates, just database tables as models.
The page is js driven and POSTing data is causing me some issues.

A user action triggers a POST that looks like:

action "update"
data { "field1": 8,"id": 2 }
(copied from firebug)

When I try to parse the POST data I get errors on every type of method
I have tried. What I eventually would like to do is something like:

if request.POST.__getitem__('xaction') == 'update':
mydata = request.POST.__getitem__('data')
model_to_update = modelname.get(id=mydata['id']
for k,v in mydata:
setattr(model_to_update, k, v)
model_to_update.save()

However every time I try to parse the "data" POST values I get errors.
I wonder if I am missing some assumptions by not importing/using
forms, or if there is a standard method to iterate through the POST
data that I am missing. I have literally tried every \*get\* dict
method from the docs and all throw errors (.list() throws numeric
indicies errors, iteritems() throws not iterable, etc.)

Any thoughts?

Thanks
Taso

Daniel Roseman

unread,
Jan 28, 2011, 2:15:37 PM1/28/11
to django...@googlegroups.com
Why on earth do you want to use __getitem__? That's an internal implementation detail, which you should never need to access in your code unless you're overriding something. POST is a dictionary-like object, which defines __getitem__ so that you can access it with the usual Python dictionary access: request.POST['data'], and so on.

Secondly, if I'm understanding what you have posted, `data` is not an element of the POST dictionary - it *is* the POST dictionary. So rather than trying to access `request.POST['data']['id']`, you should be accessing `request.POST['id']`.
--
DR.

Matias Aguirre

unread,
Jan 28, 2011, 2:15:39 PM1/28/11
to django-users
Excerpts from taso's message of Fri Jan 28 17:05:21 -0200 2011:
> Hi,

Hi,

> I am using django strictly as a backend for data processing. As such I
> am not using any forms or templates, just database tables as models.
> The page is js driven and POSTing data is causing me some issues.
>
> A user action triggers a POST that looks like:
>
> action "update"
> data { "field1": 8,"id": 2 }
> (copied from firebug)

So, it's a POST with an "action" fiend and a "data" field that looks like
a JSON value, right?

> When I try to parse the POST data I get errors on every type of method
> I have tried. What I eventually would like to do is something like:
>
> if request.POST.__getitem__('xaction') == 'update':
> mydata = request.POST.__getitem__('data')
> model_to_update = modelname.get(id=mydata['id']
> for k,v in mydata:
> setattr(model_to_update, k, v)
> model_to_update.save()
>
> However every time I try to parse the "data" POST values I get errors.
> I wonder if I am missing some assumptions by not importing/using
> forms, or if there is a standard method to iterate through the POST
> data that I am missing. I have literally tried every \*get\* dict
> method from the docs and all throw errors (.list() throws numeric
> indicies errors, iteritems() throws not iterable, etc.)
>
> Any thoughts?

Did you tried un-encoding the JSON, like this:

from django.utils.simplejson import simplejson

data = simplejson.loads(request.POST['data'])
id = data['id']
...

> Thanks
> Taso

Regards,
Matías
--
Matías Aguirre <matias...@gmail.com>

taso

unread,
Jan 28, 2011, 3:11:29 PM1/28/11
to Django users
On Jan 28, 11:15 am, Matias Aguirre <matiasagui...@gmail.com> wrote:
> Excerpts from taso's message of Fri Jan 28 17:05:21 -0200 2011:

> > A user action triggers a POST that looks like:
>
> > action "update"
> > data    { "field1": 8,"id": 2 }
> > (copied from firebug)
>
> So, it's a POST with an "action" fiend and a "data" field that looks like
> a JSON value, right?

Absolutley right. I have the option of encoding the data on the
frontend to be passed as a JSON value. Currently that is off; that is,
the flag to encode as JSON is false so the data is supposed to be
passed as a "regular" POST parameter set, even if it looks like JSON.

> Did you tried un-encoding the JSON, like this:
>
>     from django.utils.simplejson import simplejson
>
>     data = simplejson.loads(request.POST['data'])
>     id = data['id']

>
> Regards,
> Matías
> --
> Matías Aguirre <matiasagui...@gmail.com>

Absolutely works. My earlier attempt at parsing JSON was bad (new to
django + JSON). Thanks much Matías!

Best,
Taso
Reply all
Reply to author
Forward
0 new messages