django 获取 POST 请求值的几种方法

2020-07-20 社会 115阅读
1、django获取post过来的多个键值对:
Ajax:
var languages = {};
languages['english'] = ['mark', 'james'];
languages['spanish'] = ['amy', 'john'];
$.ajax({
type: 'POST',
url: '/save/',
data: languages,
dataType: 'json'
});
Django Views.py
if request.is_ajax() and request.method == 'POST':
for key in request.POST:
print key
valuelist = request.POST.getlist(key)
print valuelist
---------------------
fiddle:
name=june; age=26;
---------------------
views.py
16 for key in request.POST: 17 print key 18 valuelist = request.POST.getlist(key) 19 print valuelist
----------------------------
Development server is running at Quit the server with CONTROL-C. Your method is POST!>>>>>>>>> name [u'june']
age [u'26'] [04/Apr/2012 10:58:11] "POST /getuin/ HTTP/1.1" 200 20

2、一次加载所有值:
def view_example(request):
data=simplejson.loads(request.raw_post_data)
3、获取多个值作为一个列表
request.POST get multiple values
The QueryDict.getlist() allows to get all the checkbox(or select list) values from the request.POST/GET object.
Let’s assume we have a simple form with the following checkboxes. Each checkbox contains an ID of an artist. 1
2 ... 3 4 5 6 ... 7 </form>
In views.py : 1 def handle(request): 2 if request.method == 'POST': 3 artists = request.POST.getlist('artists') # now artists is a list of [1,2,3]
声明:你问我答网所有作品(图文、音视频)均由用户自行上传分享,仅供网友学习交流。若您的权利被侵害,请联系fangmu6661024@163.com