我的模板django中有一个表单没问题,它可以在模型中保存。那么,现在,我想在这个表单中创建一个新表单。这个表单,我在模板内创建,但现在,我想让de数据保存在另一个模型中。 (我不能使用formset,我已经准备好使用它了)。当用户单击列表中的选项时,将使用javascript创建此表单。
我正在使用基于类的视图来创建视图。
我的问题是,如何从用户动态创建的表单中获取此数据?
<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>
答案 0 :(得分:1)
在@mrnfrancesco
的评论之后,我应用以下逻辑来保存数据模型:
class CreateDemandView(FormView):
...
def form_valid(self, form):
demand = form.save(commit=False)
demand.save()
format_name = self.request.POST.getlist('format_name')
for i in format_name:
fields_name = self.request.POST.getlist('field_name_' + i)
format_name_id = NewFormatByStore.objects.get(pk=i)
data_content_field = self.request.POST.getlist('field_format_data_' + i)
j = 0
while j < len(fields_name):
format_data_demand = FomartWithDataFromDemand(demand_name=demand, format_name=format_name_id, field_name=fields_name[j], field_format_data=data_content_field[j])
format_data_demand.save()
j += 1
...
我从POST获取数据并将相应的数据保存在模型中。