I have a form in my template django that was ok, it saves ok in a model. Well, now, i want create a new form inside this form. This form, i create inside the template, but now, i want get de data to save in another model. (I can't use the formset, i'm ready using it). This form are create with javascript when user click in the option inside the list.
I'm using Class-based view to create a view.
My question is, how can i get this data from this form that was created dynamically by the user?
<div class="form-group">
<label class="col-sm-2 control-label">{{form.name.label}}</label>
<div class="col-sm-10">
{{form.name}}
{% if form.name.errors %}
<span class="glyphicon glyphicon-remove form-control-feedback"></span>
<span class="help-block">{{form.name.errors.as_text}}</span>
{% endif %}
</div>
<label class="col-sm-2 control-label">{{form.date_max.label}}</label>
<div class="col-sm-10">
{{form.date_max}}
{% if form.date_max.errors %}
<span class="glyphicon glyphicon-remove form-control-feedback"></span>
<span class="help-block">{{form.date_max.errors.as_text}}</span>
{% endif %}
</div>
<label class="col-sm-2 control-label">{{form.demand_desc.label}}</label>
<div class="col-sm-10">
{{form.demand_desc}}
{% if form.demand_desc.errors %}
<span class="glyphicon glyphicon-remove form-control-feedback"></span>
<span class="help-block">{{form.demand_desc.errors.as_text}}</span>
{% endif %}
</div>
<div class="optional-imege-form">{{formset}}</div>
<div id="create-new-image-button">Add</div>
</div>
<div class="format-list">
<h3>Lista de formatos:</h3>
<ul>
{% for key, value in format_names_fields_dict.items %}
<li class="format-create" data-format-name-id='{{key}}' data-fields-value='{% for i in value %}{{i}},{% endfor %}'>{{key}}</li>
{% endfor %}
</ul>
</div>
<div class="col-sm-12 container-with-format-forms">
</div>
<div class="ground-light-popup"></div>
<div class=""></div>
<div class="pop-up-set-store">
<label class="col-sm-3 control-label">Por favor, selecione a loja:</label>
<div class="col-sm-9">
{{form.demand_store}}
</div>
<div class="col-sm-12">
<div class="btn btn-success store-button">Loja selecionada >></div>
</div>
</div>
<div class="pop-up-set-store-2">
<h3>Selecione as áreas que será necessária na demanda</h3>
<div class="col-sm-12">
<table>
<tr>
<td>{{form.moda.label}}</td>
<td> {{form.moda}}</td>
</tr>
<tr>
<td>{{form.texto.label}}</td>
<td> {{form.texto}}</td>
</tr>
<tr>
<td>{{form.design.label}}</td>
<td> {{form.design}}</td>
</tr>
</table>
</div>
<div class="col-sm-12">
<div class="btn btn-success area-button">Área(s) selecionada >></div>
</div>
</div>
<div>
<div class="col-sm-offset-2 col-sm-10">
<input name="Criar" class="btn btn-default" type="submit" value="Criar">
</div>
</div>
<script type="text/javascript">
$(document).ready(function(){
$formatName = $('.format-create');
$formatsContainer = $('.container-with-format-forms');
for(i=0;i<$formatName.length;i++){
$textFromFormat = $($formatName[i]).text();
$textSplited = $textFromFormat.split('_');
$formatJustName = $textSplited[0];
$formatJustId = $textSplited[1];
$($formatName[i]).text($formatJustName);
}
$($formatName).click(function(){
$formatData = $(this).data();
$fieldsToFormat = $formatData.fieldsValue.split(',');
$myFormatHtml = "<form method='post' id='" + $formatJustId + "'>";
$myFormatHtml += "<div class='col-sm-12 format-content-field'>";
$myFormatHtml += "<h4>" + $(this).text() + "</h4>";
$myFormatHtml += "<input name='format_name' value='" + $formatJustId + "' type='hidden'>";
for(i=0;i<$fieldsToFormat.length-1;i++){
$fieldsToFormatName = $fieldsToFormat[i].split('_');
$myFormatHtml += "<label class='col-sm-2'>";
if($fieldsToFormatName[2] == 'True'){
$myFormatHtml += $fieldsToFormatName[0] + "* </label><input class='col-sm-10' name='field_name' id='id_field_name' type='text' required>"
}else{
$myFormatHtml += $fieldsToFormatName[0] + "</label><input class='col-sm-10' name='field_name' id='id_field_name' type='text'>"
}
}
$myFormatHtml += '</div>';
$myFormatHtml += '</form>'
$($formatsContainer).append($myFormatHtml);
$(this).addClass('ready-selected');
$(this).unbind();
});
})
</script>
request.POSTto your form class that cleans the data for you deleting your custom data added in javascript. You could accessrequest.POSTand manually extract the data you are interested in.@mrnfrancescoreference ;)