Flask submit data outside of form input fields

2.8k views Asked by At

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?

2

There are 2 answers

0
Faryus On BEST ANSWER

I figured it out. I think. Not sure if this is the proper way to do it, but it works for me.

jinja code

{% for item in items %}
    <tr>
        <form id="adauga_{{ item.stoc_id }}" action="{{ url_for('adauga', denumire_med=item.denumire_med, producator=item.producator) }}" method="POST">
            <td>{{ item.stoc_id }}</td>
            <td>{{ item.denumire_med }}</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 %}

Application code:

@richter.route("/adauga/<denumire_med>,<producator>", methods=["POST"])
def adauga(denumire_med, producator):
    unitati = request.form["unitati"]
    fractii = request.form["fractii"]
    g.db.execute("INSERT INTO stocuri_disponibile (denumire_med, producator, cant_fl, fractie) VALUES(?, ?, ?, ?)", [denumire_med, producator, unitati, fractii])
    return redirect(request.referrer)

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.

0
sooncj On

You can achieve this using HTML <input type="hidden"> tags in your template

<td><input type="hidden" name="denumire_produs" value="{{ item.denumire_produs }}">{{ item.denumire_produs }}</td>
<td>><input type="hidden" name="producator" value="{{ item.producator }}">{{ item.producator }}</td>

then retrieve it via the form in your route handler function

def adauga():
    denumire_produs = request.form["denumire_produs"]
    producator = request.form["producator"]
    ...