【发布时间】:2012-05-23 10:46:01
【问题描述】:
我有一个表单,用于添加一个项目,其中 2 个下拉列表使用与将提交表单的数据库不同的数据库填充。我想将 AJAX 添加到下拉列表中,在第一个下拉列表中选择一个项目将使用 AJAX 自动填充第二个下拉列表中的数据。问题是我对表单使用相同的视图来填充数据并且即使我使用 is.ajax() 调用它也不起作用。
这是我的 AJAX 代码:
function get_data(){
// alert('test');
new Ajax.Request('/abc/abc/add', {
method: 'POST',
parameters: $H({'type':$('id_data').getValue()},
{'csrfmiddlewaretoken':$( "csrfmiddlewaretoken" ).getValue()}),
onSuccess: function(transport) {
var e = $('id_def')
if(transport.responseText)
e.update(transport.responseText)
}
}); // end new Ajax.Request
//alert($( "csrfmiddlewaretoken" ).getValue());
}
这是我的查看代码:
if request.is_ajax():
#if request.is_ajax()
cur = connections['data'].cursor()
#auto_type = Auto.objects.filter(type=request.POST.get('type', ''))
abctype = request.POST.get('type', '')
SQL = 'SELECT uuid FROM abc_abc where uid = %s', abctype
cur.execute(SQL)
auto_type =cur.fetchone()
cur = connections['data'].cursor()
SQL = 'SELECT uuid, name FROM abc_abc where parent_id = %s', auto_type
cur.execute(SQL)
colors = cur.fetchall()
return render_to_response('abc/add_abc.html', {
'colors' : colors,
}, context_instance=RequestContext(request))
还有什么我想念的吗?如果您希望我从代码中添加更多内容,请告诉我.....请帮助!
【问题讨论】:
-
我有,这似乎工作。我正在使用网络上的 Ajax 和 django 示例之一,并从那里获取代码。 bradmontgomery.net/blog/a-simple-django-example-with-ajax
-
....而且我正在使用原型库prototypejs.org
-
您返回原始 html ('abc/add_abc.html') 是否有原因,它没有错,但我希望 json
-
不。没有理由。就像我说的那样,我正在使用上面网站的代码,并且我正在遵循那里的逻辑。我对建议持开放态度,因为我对 django/python 非常陌生,但过去曾在 Perl 中使用过 AJAX/jQuery。
标签: jquery ajax django forms python-2.7