我正在学习Flask
,并且想做一个简单的发票应用程序,但是我不知道如何返回包含以下内容的表中的所有数据行:
四个字段{code, description, price, quantity}
将作为我的数据库表的详细信息插入或更新。
这只是一个代码示例。
# 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>
答案 0 :(得分:0)
首先,如果我没记错的话,我想你想通过表单对象details=form
。
无论如何,我都没有all()方法,但是您可以在下面找到类似的内容,
if form.validate_on_submit():
save_invoice_det(form) # pass your form object
在辅助功能中,您可以看到以下内容,
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
基本上getattr(form, attr).data
是您的解决方案。
编辑:我正在使用python 3.7根据您的需要进行调整
编辑:在查看我的答案时,您也可以看到类似下面的内容,
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())
,输出为:
OrderedDict([('item_code', None), ('item_desc', None), ('quantity', None), ('price', None)])