-5

My request.POST is like this:

QueryDict: <QueryDict: {u'rows[]': [u'4f58707ba5e97c09d5000003', u'4f5881a3a5e97c0ba8000000']}>

I am trying to get those values into an array. But my code

request.POST['rows[]'] 

results only '4f5881a3a5e97c0ba8000000' Why? How can I take both of the values into an array?

Burak
  • 5,706
  • 20
  • 70
  • 110
  • Looks like a stringified Python dictionary? Do you need to handle it in JavaScript? – Alexander Pavlov Mar 06 '12 at 15:42
  • can you provide a little more context to your question? do you need to pass the dictionary to the javascript or what? – Samuele Mattiuzzo Mar 06 '12 at 15:44
  • Dictionaries are not strings, they're Python, not Django, and what you're asking is easily answered with even the most minimal of effort at your search engine of choice. But, here I'll even save you that effort: http://docs.python.org/tutorial/datastructures.html#dictionaries – Chris Pratt Mar 06 '12 at 15:46
  • If everything is solved by the search engine, why stackoverflow stands? – Burak Mar 08 '12 at 12:44
  • @ChrisPratt might want to check the accepted answer to this question. – Rob Grant Aug 21 '14 at 07:44

3 Answers3

2

QueryDict is not a standard Python dictionary. It's a subclass of MultiValueDict: https://code.djangoproject.com/browser/django/tags/releases/1.3.1/django/utils/datastructures.py#L223 You can get both items by using request.POST.getlist('rows[]').

Mark Lavin
  • 24,664
  • 5
  • 76
  • 70
1

Below is how I would do it.

my_dict = {u'en': u'description', u'tr': u'aciklama'}

print my_dict[u'en']
print my_dict[u'tr']

Something that seems wierd to me is that your dictionary keys are unicode strings. If you want to normalize them check out this answer: Fastest way to convert a dict's keys & values from `unicode` to `str`?

Community
  • 1
  • 1
Francis Yaconiello
  • 10,829
  • 2
  • 35
  • 54
0

From javascript:

var str = obj.en

From python:

str = obj['en']

or in python if you want to get 'en' or a default value in case it doesn't exist:

str = obj.get('en', 'Default Value')
Furbeenator
  • 8,106
  • 4
  • 46
  • 54