Capture a value using Select with Django

Asked

Viewed 24 times

1

Good morning. I created a form and inserted the names of my clients in a select. When selecting one of them you would like to real Select a filter, but it is not working, it seems that the value of the selected field is not being sent to the view.

Follows the code:

View

@login_required
def home_filtro(request):
    cliente = request.POST.get('cliente.id')
    atas = Ata.objects.filter(cliente=cliente)
    clientes = Cliente.objects.all()
    return render(request, 'core/index.html',
                  {'atas': atas, 'clientes': clientes)

HTML

<!--Filtros-->
<form action="{% url 'core_home_filtro' %}" method="post">
      {% csrf_token %}
      <div class="form-group">
      <select class="form-control" name="cliente_id">
              {% for cliente in clientes %}
                 <option value= "{{cliente.id}}" > {{cliente}} </option>
              {% endfor %}
      </select>
      </div>
      <button type="submit" class="mt-3 btn btn-primary">Filtrar</button>
</form>

Could you help me enter what’s going on?

Thank you

1 answer

0

If you change only the line below, see if it returns any errors.

if request.POST:
     cliente = request.POST['cliente.id']

Browser other questions tagged

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