如何使用ajax和django提交多个值

时间:2016-06-28 15:41:18

标签: javascript jquery python ajax django

问题我希望能够使用我的ajax表单提交提交多个记录。此表单是部分的,我希望每次都能提交此表单的倍数。这是我到目前为止我不能使用序列化字符串得到我的textbox.vals。任何帮助将不胜感激。

这是我的功能

function SaveDim() {

    $.ajax({
        type: "POST",
        url: "/sheet/sheet_form_create.html/_dim",
        //dataType: "json",
        data: $('#form').serialize()
            +
            "&description=" + $('#id_description').val() +
            "&style=" + $('#id_style').val() +
            "&target=" + $('#id_target').val() +
            "&upper_limit=" + $('#id_upper_limit').val() +
            "&lower_limit=" + $('#id_lower_limit').val() +
            "&inspection_tool=" + $('#id_inspection_tool').val() +
            "&critical=" + $('#id_critical').val() +
            "&units=" + $('#id_units').val() +
            "&metric=" + $('#id_metric').val() +
            "&target_strings=" + $('#id_target_strings').val() +
            "&ref_dim_id=" + $('#id_ref_dim_id').val() +
            "&nested_number=" + $('#id_nested_number').val() +
            "&posistion=" + $('#id_position').val() +
            "&met_upper=" + $('#id_met_upper').val() +
            "&met_lower=" + $('#id_met_lower').val() +
            "&valc=" + $('#id_valc').val() +
            "&sheet_id=" + $('#id_sheet_id').val() +
            "",
        success: function (json) {
            console.log(json);
            alert(json);

        }
    });
}

这是views.py

中的add_dimension方法
def add_dimensions(request):
  if request.method == 'POST':
    c_date = datetime.now()
    u_date = datetime.now()
    description = request.POST.get('description')
    style = request.POST.get('style')
    target = request.POST.get('target')
    upper_limit = request.POST.get('upper_limit')
    lower_limit = request.POST.get('lower_limit')
    inspection_tool = request.POST.get('inspection_tool')
    critical = request.POST.get('critical')
    units = request.POST.get('units')
    metric = request.POST.get('metric')
    target_strings = request.POST.get('target_strings')
    ref_dim_id = request.POST.get('ref_dim_id')
    nested_number = request.POST.get('nested_number')
    met_upper = request.POST.get('met_upper')
    met_lower = request.POST.get('met_lower')
    valc = request.POST.get('valc')
    sheet_id = request.POST.get('sheet_id')
    data = {}
    dim = Dimension(
          description=description,
          style=style,
          target=target,
          upper_limit=upper_limit,
          lower_limit=lower_limit,
          inspection_tool=inspection_tool,
          critical=critical,
          units=units,
          metric=metric,
          target_strings=target_strings,
          ref_dim_id=ref_dim_id,
          nested_number=nested_number,
          met_upper=met_upper,
          met_lower=met_lower,
          valc=valc,
          sheet_id=sheet_id, 
          created_at=c_date,
          updated_at=u_date)
    dim.save()
    data['description'] = dim.description;
    data['style'] = dim.style;
    data['target'] = dim.target;
    data['upper_limit'] = dim.upper_limit;
    data['lower_limit'] = dim.lower_limit;
    data['inspection_tool'] = dim.inspection_tool;
    data['critical'] = dim.critical;
    data['units'] = dim.units;
    data['metric'] = dim.metric;
    data['target_strings'] = dim.target_strings;
    data['ref_dim_id'] = dim.ref_dim_id;
    data['nested_number'] = dim.nested_number;
    data['met_upper'] = dim.met_upper;
    data['met_lower'] = dim.met_lower;
    data['valc'] = dim.valc;
    data['sheet_id'] = dim.sheet_id;
    return HttpResponse(json.dumps(data), content_type="application/json",)

  else:
      dim_form = DimForm()
      return render(request, 'app/_dim.html', {'dim_form': dim_form})   

2 个答案:

答案 0 :(得分:0)

您需要正确连接数据字符串,如:

data: $('#form').serialize() + "&description=" + $('#id_description').val() + "&style=" + $('#id_style').val(); // ...

答案 1 :(得分:0)

就这样做吧

function SaveDim() {

    $.ajax({
        type: "POST",
        url: "/sheet/sheet_form_create.html/_dim",
        //dataType: "json",
        //make sure you give your form an id of "dim_form"
        data: $('#dim_form').serialize()
        success: function (json) {
            console.log(json);
            alert(json);

        }
    });
}