python - 如何在 Flask Table 生成的表格列中显示货币?
问题描述
flask-table 确实有几种特定的列类型。例如日期:DateCol。但是没有货币的列类型。所以现在数据使用标准的 Col 类型显示。现在你只得到一个小数。它有效,但我更喜欢货币格式。
表.py
# import things
from flask_table import Table, Col, DateCol
# Declare my table
class MyTable(Table):
classes = ['table', 'table-hover']
id = Col('id')
amount = Col('amount')
date = DateCol('date')
模板.html
<div>{{ amounts_table }}</div>
路线.py
@route('/my_table')
def my_table():
table_content = Amounts.query.all()
amounts_table = AmountsTable(table_content)
return render_template('template.html', amounts_table=amounts_table)
结果:
id amount date
1 1,523.78 30-03-2019
我想完成什么:
id amount date
1 € 1.523,78 30-03-2019
解决方案
您可以将类子Col
类化。
假设您的amount
数据存储为字符串(例如1,523.78
),您可以执行以下操作:
# Python 3.7
import locale
class CurrencyCol(Col):
def td_format(self, content):
amount = float(content.replace(',', ''))
locale.setlocale(locale.LC_NUMERIC, 'nl_NL')
val = locale.format_string('%.2f', float(amount), 1, 1).replace(' ', '.')
return f'€ {val}'
然后更改您的表以使用新的CurrencyCol
:
class MyTable(Table):
classes = ['table', 'table-hover']
id = Col('id')
amount = CurrencyCol('amount')
date = DateCol('date')
推荐阅读
- magento2 - 升级到 Magento 2.4 后结帐/购物车订单总额和运费显示为零
- javascript - 在 array.map 中为多个元素使用 React useState
- reactjs - React SplitButton data-test-id 选项
- python-3.x - 如何在画布中滚动多个帧?
- javascript - 从构造函数中提取对象值
- geocoding - 谷歌地理编码 API 方法
- mysql - 在 MySQL 中将行转换为列
- python - 从多维数组到 Keras 中的其他形状
- ios - 开发人员在浏览代码库时会遇到两种不同的语法来创建具有给定整数的 NSString。(在 MRC 中实现)
- ios - 外部 API 链接已损坏,我无法从 iOS 开发中 Json 文件中的错误 API 获取任何数据