【发布时间】:2017-12-31 13:10:44
【问题描述】:
我的模板 django 中有一个可以的表单,它可以保存在模型中。好吧,现在,我想在这个表单中创建一个新表单。这个表单,我在模板中创建,但现在,我想获取数据以保存在另一个模型中。 (我不能使用表单集,我已经准备好使用它了)。当用户单击列表中的选项时,此表单是使用 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>
【问题讨论】:
-
Django 中的表单数据从
request.POST传递到您的表单类,该类会清理数据以删除您在javascript 中添加的自定义数据。您可以访问request.POST并手动提取您感兴趣的数据。 -
显然似乎有效,我会尝试保存在模型中,我已经通知了。
-
这行得通,如果你创建一个答案,我会把它设置为正确的。谢谢!!
-
您最好用解决您的问题的确切代码回答您自己的问题,以帮助其他编码人员解决您完全相同的问题。如果你想给我学分,你可以使用
@mrnfrancesco参考;)
标签: django python-2.7 django-forms django-templates django-class-based-views