Get dynamically created Checkbox values

Asked

Viewed 254 times

0

Developing a python application using the Flask framework for simple registration

I have the following problem, I have a register of bids and these bids can have several categories, the categories are registered by the user and appear in the registration screen of the bid in the form of checkbox for the same go selecting the appropriate categories

Until the part listing all checkboxes on the bidding screen, it was quiet and is working, but now I do not know what is the most correct way to get the ids of the requests listed. There is a simple way to go through a whole group of checkboxes, after all I do not know their id because they are created according to what is saved in the bank.

The solution I thought, was to search all the category ids in the bank and search according to the pattern I created for the name, but this does not seem to me the most correct way.

To better illustrate the problem, follow the reduced code of my html page and my functions in the controller

Html page

<form action="{{url_for('gravar_licitacao')}}" method="POST" role="form">
  <label>Categorias:</label>
  {% for categoria in categorias %}
    <input type="checkbox" id="cbCat{{ categoria._id }}" value="{{ categoria._id }}">{{categoria.nome}}       
  {% endfor %}
  <hr>
  <input type="submit" value="Enviar" class="btn btn-default"/>
</form>

Controller

@app.route("/cadastro_licitacao")
def cadastro_licitacao():
    categorias = Categoria.query.all()
    return render_template("licitacao/cadastro_licitacao.html", categorias = categorias)

@app.route("/gravar_licitacao", methods=["GET", "POST"])
def gravar_licitacao():
    if request.method == "POST":        
        #QUAL È A MANEIRA CORRETA PARA EU OBTER UMA LISTA COM AS CATEGORIAS AQUI

    return redirect(url_for("lista_licitacoes"))

1 answer

0

can get the form data like this:

if request.method == "POST":
    valor = request.form["id"]
  • The problem is that I don’t know the id of checkboxes to pass to the method. They are created dynamically and this is my problem

Browser other questions tagged

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