Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

POST object via Form-Data ( in Django)

Trying to post the data via multipart (form data) in django backend from react js.

let form_data = new FormData();
let doc = [{ "form" : 1, "city": "Bangalore"}, { "form" : 2, "city": "Delhi"}]

form_data.append("CRegNo", "Nectar00001");
form_data.append("CName", "Nectar");
form_data.append("cityName", doc);
form_data.append("userID", 1);

axios.post("http://127.0.0.1:8000/api/table/", form_data, head)

but in Django it interprets the cityName like this ['[object Object]']

Am I doing something wrong ?

like image 216
Durgendra Avatar asked Mar 22 '26 04:03

Durgendra


1 Answers

You probably should use JSON.stringify on doc as follows

form_data.append("cityName", JSON.stringify(doc));

Afterwards in your django view you need to parse the data

import json
...

city_name = json.loads(request.POST.get('cityName'))

example using class based views

import json
from django.views import View


class MyView(View):
    def post(self, request):
        city_name = json.loads(request.POST.get('cityName'))
        ....
like image 186
ihoryam Avatar answered Mar 23 '26 16:03

ihoryam



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!