避免Django的QueryDict列表限制

时间:2021-10-03 01:59:46

I'm trying to send data from a webpage to a django view to be saved as serialized json to a database. If possible, I would like to avoid django's QueryDict object and just read the request with simplejson, flatten, and save to the database. What is the best way to send the data so simplejson can flatten it?

我正在尝试将数据从网页发送到django视图,以保存为序列化的json到数据库。如果可能的话,我想避免使用django的QueryDict对象,只需用simplejson读取请求,展平并保存到数据库中。发送数据的最佳方法是什么,这样简单的json可以压扁它?

var languages = {};
languages['english'] = ['mark', 'james'];
languages['spanish'] = ['amy', 'john'];

$.ajax({
    type: 'POST',
    url: '/save/',
    data: languages,
    dataType: 'json'
});

.

if request.is_ajax() and request.method == 'POST':
    for key in request.POST:
        print key
        valuelist = request.POST.getlist(key)
        print valuelist

3 个解决方案

#1


5  

I doubt that it is possible to make django avoid creating QueryDict, but you can ignore it (from iphone Json POST request to Django server creates QueryDict within QueryDict):

我怀疑是否有可能让django避免创建QueryDict,但你可以忽略它(从iphone Json POST请求到Django服务器在QueryDict中创建QueryDict):


def view_example(request):
    data=simplejson.loads(request.raw_post_data)

#2


4  

Have you tried the QueryDict.lists() or QueryDict.dict() methods? https://docs.djangoproject.com/en/dev/ref/request-response/#querydict-objects

您是否尝试过QueryDict.lists()或QueryDict.dict()方法? https://docs.djangoproject.com/en/dev/ref/request-response/#querydict-objects

#3


1  

You can try http://code.google.com/p/jquery-json/ and make json string on client side.

您可以尝试http://code.google.com/p/jquery-json/并在客户端制作json字符串。

var languages = {};
languages['english'] = ['mark', 'james'];
languages['spanish'] = ['amy', 'john'];

var json_languages = $.toJSON(languages);//'{"plugin":"jquery-json","version":2.2}'

// '{"spanish": ["amy", "john"], "english": ["mark", "james"]}'

$.post('/save/', {data: json_languages});

in view just:

在视野中:

if request.is_ajax() and request.method == 'POST':
     data = request.POST.get('languages')

it's not the best practice, but it works fine for me sometimes.

这不是最好的做法,但它有时适合我。

#1


5  

I doubt that it is possible to make django avoid creating QueryDict, but you can ignore it (from iphone Json POST request to Django server creates QueryDict within QueryDict):

我怀疑是否有可能让django避免创建QueryDict,但你可以忽略它(从iphone Json POST请求到Django服务器在QueryDict中创建QueryDict):


def view_example(request):
    data=simplejson.loads(request.raw_post_data)

#2


4  

Have you tried the QueryDict.lists() or QueryDict.dict() methods? https://docs.djangoproject.com/en/dev/ref/request-response/#querydict-objects

您是否尝试过QueryDict.lists()或QueryDict.dict()方法? https://docs.djangoproject.com/en/dev/ref/request-response/#querydict-objects

#3


1  

You can try http://code.google.com/p/jquery-json/ and make json string on client side.

您可以尝试http://code.google.com/p/jquery-json/并在客户端制作json字符串。

var languages = {};
languages['english'] = ['mark', 'james'];
languages['spanish'] = ['amy', 'john'];

var json_languages = $.toJSON(languages);//'{"plugin":"jquery-json","version":2.2}'

// '{"spanish": ["amy", "john"], "english": ["mark", "james"]}'

$.post('/save/', {data: json_languages});

in view just:

在视野中:

if request.is_ajax() and request.method == 'POST':
     data = request.POST.get('languages')

it's not the best practice, but it works fine for me sometimes.

这不是最好的做法,但它有时适合我。