简体   繁体   English

如何在python Django request.POST中提取列表列表

[英]How to extract list of lists in python django request.POST

I have posted data from ajax, data include csrfmiddlewaretoken(string), rcount(int) and list of prices. 我已经发布了来自ajax的数据,数据包括csrfmiddlewaretoken(string),rcount(int)和价格列表。 POSTED Data to Django view looks like this. 将数据发布到Django视图如下所示。

<QueryDict: {u'prices[2][]': [u'2128', u'5', u'06/09/2016', u'12:00 AM'], u'prices[0][]': [u'2147', u'5', u'06/09/2016', u'12:00 AM'], u'rcount': [u'59'], u'prices[1][]': [u'2108', u'5', u'06/09/2016', u'12:00 AM'], u'csrfmiddlewaretoken': [u'55qQByfBUxgII2nLJCkFZHHaJjJCF70p']}>

Now when I access rcount using following syntax, it works fine. 现在,当我使用以下语法访问rcount时,它可以正常工作。

request.POST.get(rcount)

But when I try to access prices variable, It failed to get values. 但是,当我尝试访问价格变量时,它无法获取价值。

request.POST.get(prices)
or
request.POST.getlist(prices)

This code is used to post data from HTML to Django View. 此代码用于将数据从HTML发布到Django View。

var records = []

records.push([2128, 5, '06/09/2016', '12:00 AM']);
records.push([2147, 5, '06/09/2016', '12:00 AM']);
records.push([2108, 5, '06/09/2016', '12:00 AM']);

var postData = {prices: records, rcount:59, csrfmiddlewaretoken: "{{ csrf_token }}"};
var posting = $.post('/myview/setprices/', postData);

How Can I access prices list? 如何访问价目表?

From the answer of Rajesh Yogeshwar, I have used stringify and change postData as 根据Rajesh Yogeshwar的回答,我使用stringify并将postData更改为

var postData = {prices: JSON.stringify((records), rcount:59, csrfmiddlewaretoken: "{{ csrf_token }}"};
var posting = $.post('/myview/setprices/', postData);

And Now Prices are found as JSON value in request.POST and can be converted to python list using following syntax. 现在,价格在request.POST中作为JSON值找到,并且可以使用以下语法转换为python列表。

prices = request.POST.get('prices')
if prices:
    prices = json.loads(prices)

This solved my problem. 这解决了我的问题。

In continuation to my comments on your original question, you can format data in such a way 在继续我对原始问题的评论后,您可以采用以下方式设置数据格式

var records = new Array();

records.push({'key1': 2128, 'key2': 5, 'key3': '06/09/2016', 'key4': '12:00 AM'})

You can push N number of records into your records array like this. 您可以像这样将N个记录推入记录数组。

After that, you can replace this line 之后,您可以替换此行

var postData = {prices: records, rcount:59, csrfmiddlewaretoken: "{{ csrf_token }}"};

with this 有了这个

var postData = {prices: JSON.stringify(records), rcount:59, csrfmiddlewaretoken: "{{ csrf_token }}"};

In your views then, you can simply do 在您看来,您只需

prices = request.POST.get('prices', None)
if prices:
    prices = json.loads(prices)

So now prices is list of dictionaries and you can do whatever you want it 所以现在价格是字典的清单,你可以做任何你想做的

It fails because there is no 'prices' key in the dictionary you provided. 之所以失败,是因为您提供的词典中没有'prices'键。
There are instead keys 'prices[2][]' , 'prices[0][]' and 'prices[1][]' . 而是有键'prices[2][]''prices[0][]''prices[1][]' These are ordinary strings. 这些是普通的字符串。

You should check the way that this data get posted. 您应该检查此数据发布的方式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM