Snowman08 Snowman08 - 5 months ago 14
Ajax Question

Issue saving post data with ajax and django

Question I want to be able to save my partial '_dim' while viewing the main template 'sheet_form_create.html' for some reason I keep getting this error when posting the data to be saved for my '_dim' partial. Any help would be greatly appreciated.

Here is my error



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


Here is my ajax SubmitDim function



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


}
});
}


here is my sheet_form_creat.html template



{% 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 %}


here is my _dim partial template I am using crispy forms for this



{% 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>


here is my views.py with sheet_create_form and 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})

Answer

Update your add_dimension() in views.py to this *this will take care of the sheet_form key and will also save correctly :)

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