我正在使用 for 循环输出单个数据库行的列和值。这一切正常,但有几个问题。列名不适合在浏览器中输出,所以我正在寻找一种关联别名的方法(不确定这是正确的术语)
例如。列名:
cust_name
cust_area
期望的输出:
Customer name
Customer area
模型.py
class Customers(db.Model):
id = db.Column(db.Integer, primary_key = True)
cust_name = db.Column(db.String(64))
cust_area = db.Column(db.String(64))
cat_id = db.Column(db.Integer(8), index = True)
视图.py
customer = Customers.query.filter_by(cat_id = page).first()
test_dict = dict((col, getattr(test, col)) for col in test.__table__.columns.keys())
return render_template('test.html',
customer = test_dict
)
测试.html
{% for key, value in customer.items() %}
{{ key }} : {{ value }}
{% endfor %}
谢谢!