Django, retrieve selected value on page

0

Good evening!

I created a view where I retrieve the sellers and created a variable (sales_id) to receive the selected salesperson on the screen. The html is being created correctly, but the variable in the view does not receive value in POST:

View:

 def novo_orcamento(request):
    vendedores = Vendedor.objects.order_by('nome')
    id_vendedor = 0
    if request.method == 'POST':
        import pdb
        pdb.set_trace()


    context = {'vendedores':vendedores, 'id_vendedor':id_vendedor}

    return render(request, 'appOrcamento/novo_orcamento.html', context)

Html

{% block content %}
<form action="{% url 'appOrcamento:novo_orcamento' %}" method="POST">
    {% csrf_token %}
    <div class="form-group">
        <legend class="lead">PRODUTOS</legend>
        <select name=id_vendedor class="form-control">
            {% for vendedor in vendedores %}
                <option value="{{vendedor.codigo_id}}">{{vendedor.nome}}</option>
            {% endfor %}
        </select>

        <button name='submit' class="btn btn-primary">Salvar</button>
    </div>
</form>

{% endblock content %}

How do I retrieve the selected seller from the view?

    
asked by anonymous 09.03.2017 / 03:47

1 answer

2

In html, change the name of the select to use quotes:

<select name="id_vendedor">

And in your view you get the data through the request object.

def novo_orcamento(request):
    id_vendedor = request.POST.get("id_vendedor")
    #Faz os processamentos com esse valor
    
09.03.2017 / 13:41