POST jQuery的数组的Django [英] POST jQuery array to Django
本文介绍了POST jQuery的数组的Django的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想POST简单的数字的一个jQuery数组Django的,我真的不能使它发挥作用。我需要在这一点帮助。我得到一个HTTP 500错误如下:
内部服务器错误:/ edit_lists /
回溯(最近通话最后一个):
文件"/home/jabez/.virtualenvs/hackernews/local/lib/python2.7/site-packages/django/core/handlers/base.py",线187,在get_response
响应= middleware_method(请求,响应)
文件/home/jabez/.virtualenvs/hackernews/local/lib/python2.7/site-packages/django/middleware/common.py,线路106,在process_response
如果response.status_ code == 404:
AttributeError的:'名单'对象有没有属性'状态_ code'
下面是我的code:
的JavaScript
$('。BTN-组)。找到('#mark_as_done')。在('点击',函数(){
变种任务= grab_selected();
$阿贾克斯({
键入:POST,
网址:'/ edit_lists /',
数据:{任务:任务},
});
});
功能grab_selected(){
变种任务= [];
$('输入:选中)每个(函数(){。
tasks.push(this.id);
});
返回任务;
}
views.py
高清edit_lists(要求):
任务= request.POST.getlist('任务')
返回任务
urls.py
URL(R'^ edit_lists / $','todo.views.edit_lists,名字='edit_lists')
解决方案
您可以尝试使用任务[]
而不是工作$通过AJAX发送时C $ C>作为参数。例如:
$('。BTN-组)。找到('#mark_as_done')。在('点击',函数(){
变种任务= grab_selected();
$阿贾克斯({
键入:POST,
网址:'/ edit_lists /',
数据:{'任务[]':任务},
});
});
另一件事是,你只是返回返回任务
在 edit_lists()
视图,你有返回的Htt presponse
实例或使用快捷键如渲染
:
从django.http进口的Htt presponse
高清edit_lists(要求):
任务= request.POST.getlist('任务[])
返回的Htt presponse(成功)
希望它帮助,
I'm trying to POST a jQuery array of simple numbers to Django, and I really can't make it work. I need a little help on this. I'm getting an Http 500 with the following error:
Internal Server Error: /edit_lists/
Traceback (most recent call last):
File "/home/jabez/.virtualenvs/hackernews/local/lib/python2.7/site-packages/django/core/handlers/base.py", line 187, in get_response
response = middleware_method(request, response)
File "/home/jabez/.virtualenvs/hackernews/local/lib/python2.7/site-packages/django/middleware/common.py", line 106, in process_response
if response.status_code == 404:
AttributeError: 'list' object has no attribute 'status_code'
Here's my code:
JavaScript
$('.btn-group').find('#mark_as_done').on('click', function() {
var tasks = grab_selected();
$.ajax({
type: 'POST',
url: '/edit_lists/',
data: {'tasks': tasks},
});
});
function grab_selected() {
var tasks = [];
$('input:checked').each(function() {
tasks.push(this.id);
});
return tasks;
}
views.py
def edit_lists(request):
tasks = request.POST.getlist('tasks')
return tasks
urls.py
url(r'^edit_lists/$', 'todo.views.edit_lists', name='edit_lists')
解决方案
You can try to use tasks[]
instead of tasks
as parameter when sending via ajax. Example:
$('.btn-group').find('#mark_as_done').on('click', function() {
var tasks = grab_selected();
$.ajax({
type: 'POST',
url: '/edit_lists/',
data: {'tasks[]': tasks},
});
});
Another thing is you are simply returning return tasks
in edit_lists()
view, you have return a HttpResponse
instance or use shortcut like render
:
from django.http import HttpResponse
def edit_lists(request):
tasks = request.POST.getlist('tasks[]')
return HttpResponse('Success')
Hope it helps,
这篇关于POST jQuery的数组的Django的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文