0

フォームが送信されるデータベースとは異なるデータベースを使用して2つのドロップダウンが入力されるアイテムを追加するために使用されるフォームがあります。ドロップダウンにAJAXを追加したいのですが、最初のドロップダウンで1つのアイテムを選択すると、AJAXを使用して2番目のドロップダウンのデータが自動的に入力されます。問題は、フォームに同じビューを使用してデータを入力しているため、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))

私が見逃しているものは他にありますか?コードからさらに何かを追加したい場合はお知らせください.....助けてください!!

4

1 に答える 1

0

私はそれを機能させました。パラメータリストの後に2番目のパラメータを送信していた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>
于 2012-05-16T18:28:22.023 に答える