[Django]-Django, view get list of dictionaries from request

4πŸ‘

βœ…

Ensure your dataType is json, and stringify your array:

dataType: 'json',
data: {'cityId': 1, 'products_with_priority': JSON.stringify([{"id": 1, "priority": 1}, {"id": 2, "priority": 2}, {"id": 3, "priority": 3}, {"id": 4, "priority": 4}, {"id": 5, "priority": 5}])}

Then use the (standard Python library) jsonβ€˜s loads() method in your Django view:

products_priorities = json.loads(request.POST.get('products_with_priority'))
πŸ‘€nb1987

1πŸ‘

Convert the data to json string.

And receive it like

data = json.loads(request.POST)
πŸ‘€Sumeet P

1πŸ‘

send your data in a json string to django view and then
Try

def my_view(request):
    data = OrderedDict()
    data = json.loads(request.POST)
    city_id = data['city_id']
πŸ‘€TuXin Zhang

0πŸ‘

In AJAX request, make sure you include the header:

content-type: application/json

Then if you are using Python 3 as I do, in Django View:

def my_view(request):
    received_json = json.loads(request.read().decode('utf-8'))
    products_priority = received_json['products_with_priority']
πŸ‘€Vamei

0πŸ‘

If your are sending POST data as jQuery (raw) data, then you can access it via body as a string. From which you can load it as JSON. Hope the below example helps you.

def my_view(request):
    data = json.loads(request.body)
    city_id = data.get('cityId')
    products_priorities = data.get("products_with_priority")

Leave a comment