Electronics & Programming

develissimo

Open Source electronics development and programming

  • You are not logged in.

#1 Jan. 28, 2011 19:06:40

t.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

request.POST parsing without form


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
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

--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

#2 Jan. 28, 2011 19:15:45

Daniel R.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

request.POST parsing without form


On Friday, January 28, 2011 7:05:21 PM UTC, taso wrote:
>
> 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
> 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
>

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, 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`, you should be accessing
`request.POST`.
--
DR.

--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

#3 Jan. 28, 2011 19:15:49

Matias A.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

request.POST parsing without form


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
> 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)
id = data
...

> Thanks
> Taso

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

--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

#4 Jan. 28, 2011 20:11:37

t.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

request.POST parsing without form


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)
>     id = data

>
> 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

--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

Board footer

Moderator control

Enjoy the 21st of October
PoweredBy

The Forums are managed by develissimo stuff members, if you find any issues or misplaced content please help us to fix it. Thank you! Tell us via Contact Options
Leave a Message
Welcome to Develissimo Live Support