使用ajax和python使用不同的数据保存相同的表单。

时间:2016-06-30 13:58:08

标签: javascript jquery python ajax django

问题我想用ajax保存相同的表单,但是,如果他们点击一个按钮,我的表单可以是多个,它用昏暗的表单呈现它们,所以他们多次点击按钮时会出现昏暗的表单。下面的示例我想在我的数据库中添加两个dims,因此我看到了两个暗淡的表单。保存第一个可以工作但是,第二个我提交的数据与第一个相同。我试着用名字来代替id而没有运气我尝试序列化数据没有运气。任何帮助将不胜感激。

这里是SaveDim函数

function SaveDim() { 
    var dim_form = $("#dim_form :input") 
    .filter(function (index, element) { 
        return $(element).val() != ""; 
    }) 
    .serialize(); 
    alert(dim_form); 
    $.ajax({ 
        type: "POST", 
        url: "/sheet/sheet_form_create.html/_dim", 
        //dataType: "json", 
        data: 
            + 
            "&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); 
        } 
    }); 
} 

这是我的_dim视图

% load crispy_forms_tags %} 




<br /> 
<br /> 
<br /> 


<div class="my-Dim" > 
    <form id="dim_form" method="POST" action=""> {% csrf_token %} 
      {% crispy dim_form %} 
      <button class="updatedim" onclick="SaveDim()">Save Dim</button> 
    </form> 
</div> 
<div id="output"></div> 

这是我的python方法

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.objects.create( 
          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) 
    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}) 

屏幕截图以获得更好的解释

Image

0 个答案:

没有答案