Alexxio
Alexxio

Reputation: 1101

Get value in a post request, Django

am getting the following post data in my django app

POST
            Variable                      Value
            csrfmiddlewaretoken    u'LHM3nkrrrrrrrrrrrrrrrrrrrrrrrrrdd'
            id                     u'{"docs":[],"dr":1, "id":4, "name":"Group", "proj":"/al/p1/proj/2/", "resource":"/al/p1/dgroup/4/","route":"group", "parent":null'

am trying to get the id value in variable id i.e "id":4 (the value 4). When I do request.POST.get('id')I get the whole json string. u'{"docs":[],"dr":1, "id":4, "name":"Group", "proj":"/al/p1/proj/2/", "resource":"/al/p1/dgroup/4/","route":"group", "parent":null' How can I get the "id" in the string?

Upvotes: 0

Views: 966

Answers (2)

slider
slider

Reputation: 12990

The data you are sending is simply a json string.

You have to parse that string before you can access data within it. For this you can use Python's json module (it should be available if you're using Python 2.7).

import json
data = json.loads( request.POST.get('id') )
id = data["id"]

If you somehow don't have the json module, you can get the simplejson module.

For more details, refer this question : best way to deal with JSON in django

Upvotes: 1

nickzam
nickzam

Reputation: 813

That's happening because id is string, not dict as it should be. Please provide your template and view code to find source of problem.

Upvotes: 0

Related Questions