django 获取 POST 请求值的几种方法(转)
2013-08-05 18:00
381 查看
转载请注明出处:http://hi.baidu.com/leejun_2005/blog/item/9a37a22238f35c5bac34de54.html
from:http://stackoverflow.com/questions/7278523/avoiding-djangos-querydict-list-limitations
from:http://www.djangofoo.com/93/request-post-get-multiple-values
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]
from:http://stackoverflow.com/questions/7278523/avoiding-djangos-querydict-list-limitations
from:http://www.djangofoo.com/93/request-post-get-multiple-values
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]
相关文章推荐
- php发送get、post请求获取内容的几种方法
- php发送get、post请求获取内容的几种方法
- php发送get、post请求的几种方法 以及获取内容
- Servlet获取AJAX POST请求中参数以form data和request payload形式传输的方法
- Spring MVC 的请求参数获取的几种方法
- php发送get 4000 、post请求的几种方法
- springmvc请求参数获取的几种方法
- django post和get方法获取数据 数据在模板上的遍历
- SpringMVC请求参数获取的几种方法
- Spring3 MVC请求参数获取的几种方法
- php发送get、post请求的几种方法
- PHP获取POST数据的几种方法
- SpringMVC请求参数获取的几种方法
- .NET/C#发起GET和POST请求的几种方法
- Spring Controller 获取请求参数的几种方法
- Spring3 MVC请求参数获取的几种方法
- PHP获取POST数据的几种方法
- php处理json请求(php获取post请求的json数据的实现方法)
- php发送get、post请求的几种方法
- Spring MVC 的请求参数获取的几种方法