使用ajax和django发布保存发布数据的问题

时间:2016-06-22 12:24:33

标签: javascript python ajax django

问题我希望能够在查看主模板'sheet_form_create.html'时保存我的部分'_dim',因为某些原因我在发布要保存为'_dim'部分的数据时一直收到此错误。任何帮助将不胜感激。

这是我的错误

VariableDoesNotExist at /sheet/sheet_form_create.html/_dim 
Failed lookup for key [sheet_form] in u"[{'False': False, 'None': None, 'True': True}, {}]" 

Request Method: POST 

这是我的ajax SubmitDim函数

function SaveDim() { 
    $.ajax({ 
        type: "POST", 
        url: "/sheet/sheet_form_create.html/_dim", 
        dataType: "json", 
        //async: true, 
        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_posisition').val(), 
            met_upper: $('#id_met_upper').val(), 
            met_lower: $('#id_met_lower').val(), 
            valc: $('#id_valc').val(), 

        }, 
        success: function (json) { 


        } 
    }); 
} 

这是我的sheet_form_creat.html模板

    {% extends "app/layout.html" %} 
    {% load crispy_forms_tags %} 
    {% block content %} 



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


    {% csrf_token %} 
    <div class="row" > 
      {% crispy sheet_form %} 
    </div> 

    <body ng-app="dim_form"> 
      <div ng-controller="dim_ctrl"> 
          <a href="#" ng-click="show_dim()">show</a> 
            <div id="d"></div> 
      </div> 
    </body> 

    {% endblock %} 

这是我的_dim部分模板我正在为此

使用crispy表单
{% load crispy_forms_tags %} 




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


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

这是我的views.py与sheet_create_form和add_dimension

def add_dimensions(request): 
    data = {} 
    if request.method == 'POST': 
      dim_form = DimForm(request.POST) 
      if dim_form.is_valid(): 
        dim = Dimension() 
        description = dim_form.cleaned_data.get['description'] 
        style = dim_form.cleaned_data.get['style'] 
        target = dim_form.cleaned_data.get['target'] 
        upper_limit = dim_form.cleaned_data.get['upper_limit'] 
        lower_limit = dim_form.cleaned_data.get['lower_limit'] 
        inspection_tool = dim_form.cleaned_data.get['inspection_tool'] 
        critical = dim_form.cleaned_data.get['critical'] 
        units = dim_form.cleaned_data.get['units'] 
        metric = dim_form.cleaned_data.get['metric'] 
        target_strings = dim_form.cleaned_data.get['target_strings'] 
        ref_dim_id = dim_form.cleaned_data.get['ref_dim_id'] 
        nested_number = dim_form.cleaned_data.get['nested_number'] 
        position = dim_form.cleaned_data.get['posistion'] 
        met_upper = dim_form.cleaned_data.get['met_upper'] 
        met_lower = dim_form.cleaned_data.get['met_lower'] 
        valc = dim_form.cleaned_data.get['valc'] 
        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['posistion'] = dim.position; 
        data['met_upper'] = dim.met_upper; 
        data['met_lower'] = dim.met_lower; 
        data['valc'] = dim.valc; 
        data['state'] = "ok"; 
        return HttpResponse(json.dumps(data), mimetype="application/json") 
      else: 
         data = 'fail' 
         return render(request, 'app/sheet_form_create.html') 
    else: 
      dim_form = DimForm() 
      return render(request, 'app/_dim.html', {'dim_form': dim_form})      

2 个答案:

答案 0 :(得分:1)

将views.py中的add_dimension()更新为此 *这将处理sheet_form键并且还将正确保存:)

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})

答案 1 :(得分:0)

当您的form 无效时,您没有向模板发送任何上下文

  dim_form = DimForm(request.POST) 
  if dim_form.is_valid(): 
    #...
  else: 
     data = 'fail' 
     return render(request, 'app/sheet_form_create.html')

所以在app/sheet_form_create.html内,没有sheet_form

所以我猜模板中的sheet_form应该与视图中的dim_form相同?如果那是真的那么你可以:

return render(request, 'app/sheet_form_create.html', {'sheet_form': dim_form})