1

Hello, i am relatively new to Python & Django.

My Problem:

resp, content = httplib2.Http().request("http://api.xxxxx.com/api/" + username)
str_content = content.decode('utf-8')
user = json.loads(str_content)

i return the user string and try to use that on my template, the json looks like that:

{
    "XXXXXX": {
    "bans": 6,
    "ban_info": {
        "randomrandomrandom": "randomrandomrandom",
        "randomrandom": "randomrandomrandomrandom"
    }
}

how can i rotate through these items and access the random informations?

like: user['bans'] returns 6

but how do i get these random things? user['ban_info']['???'] should return


My English is not the best, sorry for that. I hope i described my problem good enough!

Thanks for every help on that topic, i also hope, that this is not a duplicate!

André Teixeira
  • 2,392
  • 4
  • 28
  • 41
  • What do you mean by "rotate"? – Rob Watts May 09 '14 at 21:00
  • Ah sorry, i mean... like a each loop. I want to get all these variables, and put them in my view, but how can i access them, i don't know the names... that's my problem –  May 09 '14 at 21:02
  • So it's that you want to get each item (which has random keys and values), not that you want to randomly choose some of them? – Rob Watts May 09 '14 at 21:07
  • Yeah, completely right. :) Sorry for that misleading description. –  May 09 '14 at 21:09

3 Answers3

2

Iterating over a dict returns its keys, for example:

pizza = {"cheese": "mozarella",
         "topping": "pepperoni"}

for key in pizza:
    print pizza[key]

# outputs: mozarella
#          pepperoni

In your example you can simply do:

for key in user['ban_info']:
    print user['ban_info'][key]
Community
  • 1
  • 1
pcoronel
  • 3,833
  • 22
  • 25
1

How about a for loop?

for x in user['ban_info']:
    print x

For keys and values:

for (k, v) in user['ban_info'].iteritems():
    print k
    print v
Tom Carrick
  • 6,349
  • 13
  • 54
  • 78
  • Yeah, but now i only get the KEYS, how do i access the values IN X? :) –  May 09 '14 at 21:12
  • Perfect, that's what i need. Just one more thing, how can i use that in my view now? If i try {% for x, y in user.ban_info %} it's not working, if i just use X without Y it works, but i don't have the values then; **GOT IT! Solution: {% for x,y in user.ban_info.items %}** –  May 09 '14 at 21:17
  • That's a template, not a view :) – Tom Carrick May 09 '14 at 21:21
  • _Argh xD. Sorry. But thanks for you help. That was insanely fast. Thank you all so much :)_ –  May 09 '14 at 21:23
0

From the views.py you should send a response with a dict of objects or in your case, the user object itself, like:

HttpResponse(simplejson.dumps(user), mimetype='application/json')

In your template you can access that dict with Django template tags like:

{{user.bans}}

Or iterate over a child array like:

{% for info in user.ban_info %}
    {{ info }}
{% endfor %}

or

{% for key, value in user.ban_info %}
    {{ key }}: {{ value }}
{% endfor %}
André Teixeira
  • 2,392
  • 4
  • 28
  • 41