Django使用AJAX与Forms,Views

我有一个表单,用于添加一个项目,其中使用不同的数据库填充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)) 

还有什么我想念的吗? 如果您希望我从代码中添加更多东西,请告诉我…..请帮助!!

我得到了它的工作。 似乎在js文件上喜欢我在参数列表后发送第二个参数。 这是新代码:

 function get_data(){ new Ajax.Request('/abc/abc/add', { method: 'POST', parameters: $H({'type':$('id_data').getValue(), 'csrftoken':$( "csrftoken" ).getValue() }), onSuccess: function(transport) { var e = $('id_data1') if(transport.responseText) e.update(transport.responseText) } }); // end new Ajax.Request } 

这是我的观点:

 if request.is_ajax(): cur = connections['data'].cursor() SQL = 'SELECT uuid, name FROM abc_abc where parent_id = %s' auto_type = request.POST.get('type','') conv = iri_to_uri(auto_type) conv2 = (conv,) cur.execute(SQL,conv2) colors = dictfetchall(cur) return render_to_response('abc/add.html', { 'colors' : colors, }, context_instance=RequestContext(request)) 

这是html obejct:

 {{ form.abc.errors }}