How to access data from a dynamic imput of a form with django?

1

I'm participating in a project where we're using django. I created a form with two initial fields and can add more dynamically using javascript.

<div class="form-group">
            <div class="panel-title"><b>Informe os Criterio:</b></div>
        </div>
        <div>
            <input type="button" id="add_field_criterio" value="adicionar">
        </div>
        <div id="grupo_criterio">
            <div>
            <label for="criterio" class="control-label">Criterio:</label>
                <input type="text" required  name="criterio[]" placeholder="Informe o nome do criterio">
            </div>
            <div>
                <label for="criterio" class="control-label">Criterio:</label>
                <input type="text" required name="criterio[]" placeholder="Informe o nome do criterio">
            </div>

$(document).ready(function() {
var campos_criterio_max = 10;
var campos_alternativa_max = 10;    //max de 10 campos
var x = 3; // campos iniciais
var y = 3;
$('#add_field_criterio').click (function(e) {
    e.preventDefault();     //prevenir novos clicks
    if (x <= campos_criterio_max) {
        $('#grupo_criterio').append('<div>\
        <label for="criterio" class="control-label">Criterio:</label>\
        <input type="text" required  name="criterio[]" placeholder="Informe o nome do criterio">\
        <a href="#" class="remover_campo_criterio btn btn-danger">Remover</a>\
        </div>');
        x++;
    }
});

// Remover o div anterior
$('#grupo_criterio').on("click",".remover_campo_criterio",function(e) {
    e.preventDefault();
    $(this).parent('div').remove();
    x--;
});

How do I access the elements of the POST method since there will be no data persistence?

    
asked by anonymous 18.02.2017 / 20:22

1 answer

0

Let's say you want to access the list of criteria that the user added:

  • Remove [] from criterio[] . When inputs are sent with even name a list is automatically created.
  • Now access the view:

    def foo(request):
           criterios = request.POST.get('criterio')
    
        
    19.02.2017 / 18:01