Reputation: 74
I'm learning Flask
and I want to do a simple invoice app, but I don't have any idea of how to return back on post all data row from a table with
four fields {code, description, price, quantity}
to be inserted or updated as the details to my DB table.
This is just a code example.
# app.py
from flask import Flask, request, redirect, url_for, render_template
from flask_wtf import FlaskForm
from wtforms import StringField, IntegerField, DecimalField
app = Flask(__main__)
def save_invoice_det():
# Here i'll iterate all rows got from the table
pass
class DetailForm(FlaskForm):
item_code = IntegerField('Item Code')
item_desc = StringField('Description')
quantity = DecimalField('Qty')
price = DecimalField('Price', places=2)
total = DecimalField('Total', places=2)
@app.route('/invoice', methods=['GET', 'POST'])
def invoice(number):
form = DetailForm()
# This is just example models table
query_det = Details_Inv.query.get(number)
if request.method == 'POST':
# Here i need to know how to get all row from the table
if form.validate_on_submit():
save_invoice_det()
return redirect(url_for('list_invoices'))
return render_template('invoice.html', details=query_det)
if name == '__main__':
app.run(debug=True)
# invoice.html
<!DOCTYPE html>
<html lang='en'>
<head>
<meta charset='UTF-8'>
<title>Create / Edit Invoice</title>
</head>
<body>
<form method='POST'>
<table> <!-- This table with is what i want to return back in a request -->
<thead>
<tr>
<th>Item Code</th>
<th>Item Descripction</th>
<th>Quantity</th>
<th>Price</th>
<th>Total</th>
</tr>
</thead>
<tbody>
{% for detail in details %}
<tr>
<td>{{ detail.item_code }}</td>
<td>{{ detail.item_desc }}</td>
<td>{{ detail.quantity }}</td>
<td>{{ detail.price }}</td>
<td>{{ detail.total }}</td>
</tr>
{% endfor %}
</tbody>
</table>
</form>
</body>
</html>
Upvotes: 1
Views: 6211
Reputation: 126
first of all, if i am not wrong i guess you want to pass details=form
, your form object.
Anyways there is no all() method that i know of, but you can have something like below,
if form.validate_on_submit():
save_invoice_det(form) # pass your form object
and in worker function you can have something as below,
attrs = vars(form)
for attr in attrs:
if 'csrf_token' in attr:
continue
try:
print(f'for {attr} field data is {getattr(form, attr).data}')
except:
pass
Basically getattr(form, attr).data
is your solution.
Edit: I am using python 3.7 make adjustment according to your need
Edit: While looking at my answer you can have something like below too,
from flask_wtf import FlaskForm
from wtforms import StringField, IntegerField, DecimalField
from flask import Flask
from collections import OrderedDict
app = Flask(__name__)
app.secret_key = b'_5#y2L"F4Q8z\n\xec]/'
class CustomFlaskForm(FlaskForm):
ignore_field = ['csrf_token']
def all(self):
val = OrderedDict()
attrs = vars(self)
for attr in attrs:
if attr in self.ignore_field:
continue
try:
val[attr] = getattr(self, attr).data
except Exception as err:
pass
return val
class DetailForm(CustomFlaskForm):
def __init__(self):
super(DetailForm, self).__init__()
self.ignore_field.append('total')
item_code = IntegerField('Item Code')
item_desc = StringField('Description')
quantity = DecimalField('Qty')
price = DecimalField('Price', places=2)
total = DecimalField('Total', places=2)
with app.test_request_context() as app:
form = DetailForm()
print(form.all())
and output is:
OrderedDict([('item_code', None), ('item_desc', None), ('quantity', None), ('price', None)])
Upvotes: 1