我在我的应用程序中使用内联formset来添加和删除广告系列,但是有一个字段“显示”,如果有100个已注册的显示,则很难注册20个内联。所以我创建了一个带有jquery的侧边栏,我在其中选择扇区并显示下面的显示。想法是单击“添加”按钮(Adicionar)并创建一个填充了“display”字段的内联显示。 如何创建一个内联formset,字段“display”将填充侧栏中的所选显示?
图像如何: http://postimage.org/image/3qp6bq5f9/
在附文栏中我有:
list.append('<ul><li><input class="list-display" type="hidden" value="'+d.pk+'"> '+d.fields['nome']+' | Janelas: '+d.fields['janelas']+'\
<a class="btn btn-mini" onclick="addInlineFormset('+d.pk+');" value="Adicionar"><i class="icon-plus-sign"></i> Adicionar</a></li></ul>');
“d.pk”是所选显示的ID。
我仍在尝试创建此内联。 我在form.html中添加了一个include:
<div id="janela">
{% include "project/campanha/display_inline.html" %}
</div>
这是ajax:
function addJanela(id) {
var object_id = id;
var url = '/project/campanha/get_formInline/'+object_id+'/';
$.ajax({
type: 'get',
url: url,
success: function(formset){
var janela = $('#janela');
janela.append(formset);
return false;
},
});
}
我的views.py:
def get_formInline(request, object_id):
display = Display.objects.get(pk=object_id)
status = Status.objects.filter(nome__exact='Vendido')
initial=[{'postais' : 45, 'status': status,'display': display,}]
JanelaFormSet = inlineformset_factory(Campanha, Janela, extra=1)
formset = JanelaFormSet(initial=initial)
return render_to_response('project/campanha/display_inline.html', {'formset': formset,}, context_instance=RequestContext(request))
但是在提交中只保存最后一个内联。 form.html的源代码:
<input type="hidden" name="janela_set-TOTAL_FORMS" value="1" id="id_janela_set-TOTAL_FORMS" /><input type="hidden" name="janela_set-INITIAL_FORMS" value="0" id="id_janela_set-INITIAL_FORMS" /><input type="hidden" name="janela_set-MAX_NUM_FORMS" id="id_janela_set-MAX_NUM_FORMS" />
<table border="0" cellpadding="0" cellspacing="0">
<tbody>
<input type="hidden" name="janela_set-0-id" id="id_janela_set-0-id" />
<tr>
<td>Postais: <input type="text" name="janela_set-0-postais" value="45" id="id_janela_set-0-postais" /></td>
<td>Status: <select name="janela_set-0-status" id="id_janela_set-0-status">
<option value="" selected="selected">---------</option>
<option value="2">Girando</option>
<option value="1">Vendido</option>
</select></td>
<td>Display: <select name="janela_set-0-display" id="id_janela_set-0-display">
<option value="" selected="selected">---------</option>
<option value="1">Shopping Point</option>
<option value="2">Restaurante</option>
<option value="3">Centro de Artes</option>
</select></td>
<td><input type="checkbox" name="janela_set-0-DELETE" id="id_janela_set-0-DELETE" /></td>
在图像打印中显示3个内联但在源中仅显示1个内联。 = /
有人可以帮忙吗? 谢谢!
答案 0 :(得分:0)
解决了这段代码:
// Ajax function
$("#add").live("click", function() {
var add = 'add';
var total_forms = $('#id_janela_set-TOTAL_FORMS').val();
var initial_forms = $('#id_janela_set-INITIAL_FORMS').val();
var max_forms = $('#id_janela_set-MAX_NUM_FORMS').val();
var display_id = $(this).val();
var url = '{% url "add_campanha" %}';
$.ajax({
type: 'post',
url: url,
data: {
'janela_set-TOTAL_FORMS': total_forms,
'janela_set-INITIAL_FORMS': initial_forms,
'janela_set-MAX_NUM_FORMS': max_forms,
'add': add,
'display_id': display_id,
'csrfmiddlewaretoken':'{{ csrf_token }}',
},
success: function(data){
//alert(data);
var janela = $('#janela');
janela.html(data);
//cloneMore('#form-campanha tbody tr', 'janela_set');
console.log('Success!');
},
});
});
查看:
if 'add' in request.POST:
cp = request.POST.copy()
cp['janela_set-TOTAL_FORMS'] = int(cp['janela_set-TOTAL_FORMS'])+ 1
formset = PrimaryFunctionFormSet(cp, prefix='janela_set')
return render_to_response('project/campanha/display_inline.html', {'formset':formset}, context_instance=RequestContext(request))
使用include html渲染内联的模板:
<div id="janela">
{% include "project/campanha/display_inline.html" %}
</div>
包含display.html模板:
{{ formset.management_form }}
<table border="0" cellpadding="0" cellspacing="0">
<tbody>
{% for form in formset %}
{{ form.id }}
<tr>
<td>Postais: {{ form.postais }}</td>
<td>Status: {{ form.status }}</td>
<td>Display: {{ form.display }}</td>
{% if formset.can_delete %}
<td>{{ form.DELETE }}</td>
{% endif %}
</tr>
{% endfor %}
</tbody>
</table>
append()中的另一个问题是这篇帖子中的formset: https://stackoverflow.com/questions/10131257/django-and-append-jquery-with-inline-formset-problems