Ajax + Django - 将“数据”中的form.serialize和stringized JSON对象合并参数 [英] Ajax + Django - combining form.serialize and stringified JSON object in "data" parameter
问题描述
我正在尝试将json参数和序列化格式传递给Django视图,如下所示:
var selected_items = legalEntitiesVendorAgreements_selectize.items;
var data = $ form.serializeArray();
for(var i = 0; i< selected_items.length; ++ i)
{
data.push({'legal_entity_own_id':selected_items [i]});
}
查看:
def my_view(request):
list_data = json.loads(request.body)
for x在list_data中:
//做smth with x [ 'some_predefined_field']
基本上我在这里有两个大问题:
- 如何在数据参数
- 中结合$ m_form和json_str我的Django视图代码如何在解析请求参数。具体来说,将$
json.loads(request.body)
和for循环仍然工作,并将Django语法my_form = MyForm(request.POST)
仍然有效
即使从什么时候开始也不知道。我已经检查了这个,但我有一个不好的感觉, code> $ m_form + json_str 不是正确的方法。请帮助我!
您正在尝试混合和匹配表单数据和JSON。您需要选择一个,并将另一个转换为该格式。
例如,您可以在序列化之前将表单数据添加到obj中:
form_data = $ m_form.serializeArray();
json_obj [form_data] = form_data;
json_str = JSON.stringify(json_obj)
现在您的Django视图可以通过 list_data []访问反序列化的表单数据form_data]
。
或者,您可以以标准方式发布表单,JSON作为该表单中的值: p>
form_data = $ m_form.serialize();
form_data [json_data] = json_obj;
$ .post(url,form_data);
现在您的Django视图将获得标准的POST字典,您可以正常访问其大部分元素但是具体地反序列化json_data元素。
form = MyForm(request.POST)
json_data = json.loads .POST [json_data])
(窗体应该忽略附加字段) p>
I am trying to pass stringified json parameter and serialized form into Django view like this:
JS:
var selected_items = legalEntitiesVendorAgreements_selectize.items;
var data = $form.serializeArray();
for (var i = 0; i < selected_items.length; ++i)
{
data.push({'legal_entity_own_id' : selected_items[i]});
}
View:
def my_view (request):
list_data = json.loads(request.body)
for x in list_data:
// Do smth with x['some_predefined_field']
Basically, I have two big questions here:
- How do I combine $m_form and json_str in the data parameter
- How does my Django view code change in the part of parsing the request parameter. Specifically, will
json.loads(request.body)
and the for cycle still be working, and will Django syntaxmy_form = MyForm(request.POST)
still be valid
And don't know even when to start from. I've examined this, but I've got a bad feeling that $m_form + json_str
is not the right way here. Help me please!
You're trying to mix and match form data and JSON here. You need to choose one, and convert the other into that format.
For example, you could add your form data into the obj before serializing:
form_data = $m_form.serializeArray();
json_obj["form_data"] = form_data;
json_str = JSON.stringify(json_obj);
and now your Django view can access the deserialized form data via list_data["form_data"]
.
Or, you could post your form in the standard way, with the JSON as a value inside that form:
form_data = $m_form.serialize();
form_data["json_data"] = json_obj;
$.post(url, form_data);
Now your Django view will get a standard POST dictionary, and you can access most of its elements as normal but specificially deserialize the json_data element.
form = MyForm(request.POST)
json_data = json.loads(request.POST["json_data"])
(the form should just ignore the additional field).
这篇关于Ajax + Django - 将“数据”中的form.serialize和stringized JSON对象合并参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!