json parsing and db insert problem

123 views
Skip to first unread message

Yebach

unread,
Jul 25, 2014, 3:22:25 AM7/25/14
to web...@googlegroups.com
Hello

OK can smbd. explains this S4!#T to me.
I am sending json string from my view to controller. in request.vars['value'] there is a string /dict and for easier handling I want to convert it to a dict. 
Now this code works.

def saveAsFormData():
        import json 
#data =  simplejson.loads(request.vars.value)
#print data
#datumDo = data["dateTo"]
#datumOd = data["dateFrom"]
data = request.vars['value']
name = request.vars['name']
data_dict =  json.loads(data)
datumOd = data_dict["dateFrom"]
datumDo = data_dict["dateTo"]
schedule = {}
schedule['from'] = '1970-01-01'
schedule['to'] = '1970-01-01'
return
id = db.script.insert(sc_name = name, sc_user = auth.user_id, sc_organization = auth.user.organization, sc_cal_start = datumOd,\
sc_cal_end = datumDo, sc_status = 1, sc_modified = 'now')
return_data = {
'type': 1,
'msg': 'aaa',
'id': id
}
print "New script inserted into db"
return dict(return_data)

this code 

def saveAsFormData():
import json 
#data =  simplejson.loads(request.vars.value)
#print data
#datumDo = data["dateTo"]
#datumOd = data["dateFrom"]
data = request.vars['value']
name = request.vars['name']
data_dict =  json.loads(data)
datumOd = data_dict["dateFrom"]
datumDo = data_dict["dateTo"]
schedule = {}
schedule['from'] = '1970-01-01'
schedule['to'] = '1970-01-01'

id = db.script.insert(sc_name = name, sc_user = auth.user_id, sc_organization = auth.user.organization, sc_cal_start = datumOd,\
sc_cal_end = datumDo, sc_status = 1, sc_modified = 'now')
return_data = {
'type': 1,
'msg': 'aaa',
'id': id
}
print "New script inserted into db"
return dict(return_data)

return an error 

<type 'exceptions.TypeError'> expected string or buffer

in line data_dict =  json.loads(data)

WHY?????? why does insert or smth crashes it on line before it is executed???

Manuele Pesenti

unread,
Jul 25, 2014, 4:08:35 AM7/25/14
to web...@googlegroups.com
Il 25/07/14 09:22, Yebach ha scritto:

<type 'exceptions.TypeError'> expected string or buffer

in line data_dict =  json.loads(data)

WHY?????? why does insert or smth crashes it on line before it is executed???
can you paste a print of your data variable value just before the code crashes?

    M.

Vid Ogris

unread,
Jul 25, 2014, 4:17:11 AM7/25/14
to web...@googlegroups.com
Found the problem

The error was the second call for json object when I got an error in console, so I went to check the error folder in app. Looks like I have a date parse problems. 


--
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
---
You received this message because you are subscribed to a topic in the Google Groups "web2py-users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/web2py/b7-CaacVqMI/unsubscribe.
To unsubscribe from this group and all its topics, send an email to web2py+un...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Lep pozdrav 

Vid Ogris


Derek

unread,
Jul 25, 2014, 2:51:01 PM7/25/14
to web...@googlegroups.com
I don't get it. Why exactly are you passing around json? Your usage doesn't warrant it, and it looks like it is causing you problems. Why not send it like regular x-www-form-urlencoded?
Reply all
Reply to author
Forward
0 new messages