I'm working with Flask, and I am trying to submit some data that is not in input fields, to be added to the database.
jinja code with the form(s) looks like this:
{% for item in items %}
<tr>
<form id="adauga_{{ item.stoc_id }}" action="{{ url_for('adauga') }}" method="POST">
<td>{{ item.stoc_id }}</td>
<td>{{ item.denumire_produs }}</td>
<td>{{ item.producator }}</td>
<td>{{ item.data_expirarii }}</td>
<td>{{ item.cant_fl }}</td>
<td>{{ item.fractie }}</td>
<td>{{ '%0.2f' | format(item.pret_fl | float) }}</td>
<td><input type="number" max="{{ item.cant_fl }}" class="form-control" name="unitati"></td>
<td><input type="number" max="{{ item.fractie }}" class="form-control" name="fractii"></td>
<td><button type="submit" class="btn btn-primary" form="adauga_{{ item.stoc_id }}"><i class="fa fa-plus"></i> ADAUGA</button></td>
</form>
</tr>
{% endfor %}
And in my app code I have this
@richter.route("/adauga", methods=["POST"])
def adauga():
denumire_produs = (how do I get this?)
producator = (how do I get this?)
unitati = request.form["unitati"]
fractii = request.form["fractii"]
g.db.execute("INSERT INTO stocuri_disponibile (denumire_produs, producator, cant_fl, fractie) VALUES(?, ?, ?, ?)", [denumire_produs, producator, unitati, fractii])
return redirect(request.referrer)
How can I get the denumire_produs and producator variables from the form?
I figured it out. I think. Not sure if this is the proper way to do it, but it works for me.
jinja code
Application code:
If there's a better way, please share. I have like... 15 arguments to pass in total, so I'm curious if there's a shorter way to do it.
Cheers.