Get dynamically created Checkbox values

0

Developing a python application using the Flask framework to perform a simple registration

I have the following problem, I have a list of bids and these bids can have several categories, the categories are registered by the user and appear on the screen of the bidding record in the form of a checkbox to the same go selecting the appropriate categories

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

The solution I thought was to get all the category ids in the database and go looking for the pattern that I named the name, but that does not seem to me the right 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"))
    
asked by anonymous 10.04.2018 / 21:45

1 answer

0

You can get the form data like this:

if request.method == "POST":
    valor = request.form["id"]
    
10.04.2018 / 22:28