0

我的应用程序中有一个内联表单集,用于添加和删除广告系列,但有一个“显示”字段,如果有 100 个已注册的显示,则很难注册 20 个内联。因此,我使用 jquery 创建了一个侧边栏,在其中选择扇区并显示下面的显示。这个想法是单击“添加”按钮(Adicionar)并创建一个内联显示,其中填充了“显示”字段。如何创建一个内联表单集,其中“显示”字段填充侧栏中的选定显示?

图片如何: 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 中添加了一个包含:

<div id="janela">
{% include "project/campanha/display_inline.html" %}    
</div>

这是阿贾克斯:

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;
   },
}); 
}

我的意见.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 个内联。=/

有人可以帮忙吗?谢谢!

4

1 回答 1

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))

使用包含 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() 这篇文章中的表单集的另一个问题: https ://stackoverflow.com/questions/10131257/django-and-append-jquery-with-inline-formset-problems

于 2012-04-12T20:57:43.027 回答