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

 我来答
己学好4
推荐于2016-11-12 · TA获得超过1.5万个赞
知道大有可为答主
回答量:1.1万
采纳率:91%
帮助的人:5068万
展开全部
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 http://192.168.1.102:8357/ 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 <form method="post" action=""> 2 ... 3 <input value="1" name="artists" type="checkbox"> 4 <input value="2" name="artists" type="checkbox"> 5 <input value="3" name="artists" type="checkbox"> 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]
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式