Pass json and deserialize form in django

随声附和 提交于 2019-12-04 10:38:31

The form data is not encoded in JSON but as a query string. You can use urlparse.parse_qs from the standard library to parse that query string.

Example:

from urlparse import parse_qs


form_data = parse_qs(request.POST['form'].encode('ASCII'))  # Query strings use only ASCII code points so it is safe.

# To access address_city:
address_city = form_data['address_city'][0].decode('utf-8')

# If you plan to pass form_data to a Django Form,
# use QueryDict instead of parse_qs:
from django.http import QueryDict


form_data = QueryDict(request.POST['form'].encode('ASCII'))

Solution 1

Alternatively to aumo 's answer, and assuming you are using jQuery, you may actually want to use .serializeArray() instead of .serialize(), to generate a JSON object that you can then JSON.stringify and POST.

data = {
  form : $(this).serializeArray(),
  some_array: [2,3,4,1]
};

$.ajax(url, {
  data: JSON.stringify(data),
  contentType: 'application/json',
  type: 'POST',
});

As you've pointed out in comments, the format of serializeArray JSON output is not easily usable by a Django form, and requires further processing, after deserializing the content through json.load.

data = json.load(request)
value = data['some_array']

form_data = {}
for f in data['form']:
    if f not in form_data:
        form_data[f['name']] = f['value']
    elif not isinstance(form_data[f['name']], list):
        form_data[f['name']] = [form_data[f['name']], f['value']]
    else:
        form_data[f['name']].append(f['value'])

form = MyForm(data=form_data)

Solution 2

After taking another look at the code, it might be better (and more generic for future safeness) to serialize the form data to proper json format in the frontend.

Although there are many solutions to this; my best bet would be to go with a tried/popular library such as jquery-serialize-object, the code change would be small:

<script src="jquery.serialize-object.min.js"></script>
<script>
    data = {
      form : $(this).serializeObject(),
      some_array: [2,3,4,1]
    };

    $.ajax(url, {
      data : JSON.stringify(data),
      contentType : 'application/json',
      type : 'POST',
    });
</script>

And then the server code would be much simpler

data = json.load(request)
value = data['some_array']
form = MyForm(data=data['form'])
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!