javascript - Issue's saving multiple records with ajax in django -
i having issue's saving form multiple times. want user fill out form many dimensions possible(this form) so, if did once insert 1 record db if 2 dimensions save each 1 db totaling 2 new records. figuring out appreciated.
here ajax
function savedim() { $.ajax({ type: "post", url: "/sheet/sheet_form_create.html/_dim", //datatype: "json", data: $('#dim_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); } }); }
here views.py (add_dimension) method
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})
model
class dimension(models.model): description = models.charfield(max_length=255) style = models.charfield(max_length=255) created_at = models.datefield() updated_at = models.datefield() target = models.integerfield() upper_limit = models.integerfield() lower_limit = models.integerfield() inspection_tool = models.charfield(max_length=255) critical = models.integerfield() units = models.charfield(max_length=255) metric = models.charfield(max_length=255) target_strings = models.charfield(max_length=255) ref_dim_id = models.integerfield() nested_number = models.integerfield() #position = models.integerfield() met_upper = models.integerfield() met_lower = models.integerfield() valc = models.charfield(max_length=255) sheet = models.foreignkey(sheet, on_delete=models.cascade, default=default_foreign_key)
change change dim.save()
dimension.objects.create()
https://stackoverflow.com/a/23926742/475565
you should change id's classes
$('.dim_form').on('submit', function(){ var data = $(this).serialize(); // send ajax call here. $(this) form instance var description = $(this).find('input[name=description]').val(); ... });
Comments
Post a Comment