Py学习  »  问与答

怎么把Django的{{context}}作为参数传给ajax???

HelloSam • 8 年前 • 1901 次点击  

我想把Django的{{ context }}传给ajax,代码如下(重点是在botton那里):

<form method='POST' class='form-horizontal'>
{% csrf_token %}
<div class="tab-pane active" id="xiaoqu">
<div class='form-group'>
<label class='control-label col-md-2'>{{ district_form_context.district_name.label }}</label>
    <div class='col-md-4'>
        {{ district_form_context.district_name }}
    </div>
</div>
</div>
<div class='form-group col-md-2'>
<input type='button'  style='margin-left: 329px;' class='btn btn-primary' value='添加'
        onclick="addDistrictAction({{ district_form_context }})"/>
</div>
</form>

然后我在ajax是这样接收的:

function addDistrictAction(context){
district_form_context= context;
$.ajax({
    url:'setupAjax/addDistrictActionAjax.action',
    type:'post',
    dataType:'html',
    success:function(result){
        if(result != '')
        {
            getAllDistrict();
        }
        else{
            addDistrict()
        }
    },
    error:function(){
        alert('服务器错误');
    }
});}

但这样行不通,我很想截图给你们看但是不知道这里该怎么截图……总之这样是错的,请问我该怎么把{{ context}}的 值传给ajax???

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/1511
 
1901 次点击  
文章 [ 1 ]  |  最新文章 8 年前
巴蒂很行-weibo
Reply   •   1 楼
巴蒂很行-weibo    8 年前

{{ district_form_context }} 当然不行了! javascript能理解的数据。$.ajax({ url:'setupAjax/addDistrictActionAjax.action', type:'post', dataType:'html', data: .....

}