【问题标题】:Django using AJAX with Forms, ViewsDjango 使用带有表单、视图的 AJAX
【发布时间】: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


【解决方案1】:

我让它工作了。似乎喜欢我在参数列表之后发送第二个参数的 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对象:

<table border="0" cellpadding="0" cellspacing="0">
        <tr>{{ form.abc.errors }}</tr>
        <tr>
            <th><label>ABC:</label></th>
            <td><select name="abc" id="id_abc">
  <option value="" selected="selected">---------</option>
 {% for c in colors %}
<option value="{{ c.uuid }}">{{ c.name }}</option>
    {% endfor %}
</select></td>
            <td></td>
        </tr>
    </table>
    <br>

【讨论】:

    猜你喜欢
    • 2017-11-16
    • 2012-02-22
    • 1970-01-01
    • 2012-11-09
    • 2013-07-24
    • 2015-02-03
    • 2016-05-02
    • 2016-10-15
    相关资源
    最近更新 更多