Django, recover selected value on page

Asked

Viewed 1,390 times

1

Good night!

I created a view where I recover the sellers and created a variable (id_seller) to receive the selected seller on the screen. html is being created correctly, but the variable in the view does not receive value in the 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 recover the selected seller in the view?

1 answer

2


In html changes the name of select to use quotes:

<select name="id_vendedor">

And in your view take the data through the request object.

def novo_orcamento(request):
    id_vendedor = request.POST.get("id_vendedor")
    #Faz os processamentos com esse valor
  • 1

    Marlysson, thank you for your help. It worked perfectly, I will end my doubt with your reply.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.