Pass json and deserialize form in django -
the issue next: send post data ajax server. data looks like:
data = { form : $(this).serialize(), some_array: [2,3,4,1] }
how form object in django? request.post['form']
returns string form. i'm trying use import json
library.
but, when run value = json.load(request.post['some_array'])
or form = json.load(request.post['form'])
doesn't work.
printing request.post['form']
returns following:
u'csrfmiddlewaretoken=i3lwajfhzrgyd5ns3m9xcjkfklxnhxor& address_city=%d0%9a%d0%b8%d1%97%d0%b2& address_street=2& address_building=3& delivery_time=2015-05-15'
the form data not encoded in json query string. can use urlparse.parse_qs
standard library parse query string.
example:
from urlparse import parse_qs form_data = parse_qs(request.post['form'].encode('ascii')) # query strings use ascii code points safe. # access address_city: address_city = form_data['address_city'][0].decode('utf-8') # if plan pass form_data django form, # use querydict instead of parse_qs: django.http import querydict form_data = querydict(request.post['form'].encode('ascii'))
Comments
Post a Comment