19874

Read a multidimensional post request using Django / Python

Question:

I'm sending a post request like this:

photo[1][id] = 1234 photo[1][size] = 4x4 photo[1][quantity] = 2 photo[2][id] = 4567 photo[2][size] = 4x6 photo[2][quantity] = 1 ...

What is the best way to read this data using Django/Python?

Thanks!!

Answer1:

You might want to try <a href="https://github.com/bernii/querystring-parser" rel="nofollow">querystring-parser</a>.

For example, if you had the following form being submitted via POST to your view:

<input name="photo['1']['id']" value="1234"> <input name="photo['1']['size']" value="4x4"> <input name="photo['1']['quantity']" value="2"> <input name="photo['2']['id']" value="4567"> <input name="photo['2']['size']" value="4x6"> <input name="photo['2']['quantity']" value="1">

In your view you can parse it like this:

from querystring_parser import parser post_dict = parser.parse(request.POST.urlencode()) print post_dict # {u'csrfmiddlewaretoken': u'<crazy hash goes here>', # u'photo': # {1: {u'id': u'1234', u'size': u'4x4', u'quantity': u'2'}, # 2: {u'id': u'4567', u'size': u'4x6', u'quantity': u'1'} # }

Accessing the first photo's size is as simple as post_dic[1]['size']

Recommend

  • How to add new collection criteria in Plone 5?
  • Django how to modify database records by template
  • Process.StartTime Access Denied
  • Getting JavaScript runtime error: irrationalPath, what does it mean?
  • mail server requires authentication when attempting to send to a non-local e-mail address when using
  • proxy request in node.js / express
  • BizTalk Party not being resolved for incoming HL7v2 message
  • HTML checkbox form and HTTP URL
  • The symbol you provided is not a function
  • Why is django manage.py syncdb failing to create new columns on my development server?
  • Django return user model id with L
  • Is it possible to run clang with llc flags
  • import scipy.sparse failed
  • During installation of Django, why do I keep getting ImportError: No module named django?
  • where do I find the xml.dom python package for the python-2.6.0-8.9.28 and I have a suse/x86_64 vers
  • Python pickle not one-to-one: different pickles give same object
  • Adding elements to a huge XML file
  • Django simple Captcha “No module named fields” error
  • How integrated is Collada to OpenGL ES
  • why xml file does not aligned properly after append the string in beginning and end of the file usin
  • htaccess add www if not subdomain, if subdomain remove www
  • Jackson Parser: ignore deserializing for type mismatch
  • JSON response opens as a file, but I can't access it with JavaScript
  • Accessing IRQ description array within a module and displaying action names
  • Why doesn't :active or :focus work on text links in webkit? (safari & chrome)
  • jQuery tmpl and DataLink beta
  • InvalidAuthenticityToken between subdomains when logging in with Rails app
  • SQL merge duplicate rows and join values that are different
  • Proper way to use connect-multiparty with express.js?
  • Free memory of cv::Mat loaded using FileStorage API
  • How to set the response of a form post action to a iframe source?
  • Memory offsets in inline assembly
  • embed rChart in Markdown
  • Turn off referential integrity in Derby? is it possible?
  • LevelDB C iterator
  • Can't mass-assign protected attributes when import data from csv file
  • How to get NHibernate ISession to cache entity not retrieved by primary key
  • How can I use `wmic` in a Windows PE script?
  • Unable to use reactive element in my shiny app
  • jQuery Masonry / Isotope and fluid images: Momentary overlap on window resize