Flask如何使用LinkCol对数据库查询数据结果?

How use Flask LinkCol on database query data results?

我使用 Flask 已经有一段时间了,并为我工作的部门开发了一个数据库管理器应用程序来管理内部数据(我不是“真正的”开发人员,但通过 doing/necessity 学习) .该应用程序作为基本的 CRUD 应用程序运行良好。在其中,我使用了 Flask 表单和 tables,包括用于 linking 应用程序中不同功能的 LinkCol 列。在这些情况下,我使用 'Edit' 或 'Delete' 可点击 link;这意味着 table 中的每一列都有一个带有该词的额外单元格。单击其中任何一个都会将用户带到相应的页面,他们可以在该页面上对该项目执行适当的操作。

目前我正在尝试使用 LinkCol,以便从数据库返回的数据可以是可点击的 link,link 到另一个函数而不是每个 table 都有一个额外的列(例如,“转到帐户”)。因此,当我的 table 填充帐号时,用户可以单击 table 中的实际帐号以转到不同的页面以查看有关该特定帐户的其他数据。我阅读了文档并查看了创建者页面上关于覆盖 类 内容的示例,但一直无法弄清楚。我自己写了一些 类,但无论如何我都不是这方面的专家。我也找不到任何其他可能有帮助的东西,包括在 SO 上,这总是我用来解决问题的资源。

这是我的 table 定义,它使用的 LinkCol 呈现不正确,并在每个单元格中放置 'Account #' 而不是我想要的动态数据。更改为 Col 可以正确显示,但我希望它可以点击:

class AccountsResults(MainTable):
    AccNo = LinkCol('Account #','dbhome_bp.route.account_detail', url_kwargs=
           dict(AccNo='AccNo',StatusCode='Active',ServiceAddress1=\
           'ServiceAddress1'))
    TnCount = Col('Total TNs')
    ServiceAddress1 = Col('Service Address')
    ServiceCity = Col('Service City')
    ServiceZip = Col('Service Zip Code')
    MasterServiceDate = Col('Master Service Date')
    Active = Col('Currently Active')

这里是我知道我能做什么和我想做什么的例子。 可以做('Go To Account' 可在转到帐户列中单击):

Go To Account Account # Service Address Etc.
Go To Account 12345678 1234 Anywhere Yup
Go To Account 12345679 5678 Somewhere Nah

想做(帐号可以在账户#栏点击):

Account # Service Address Etc.
12345678 1234 Anywhere Yup
12345679 5678 Somewhere Nah

有谁知道如何使 Flask Table 中的 LinkCol 从查询返回的动态数据可点击 link?

非常感谢任何帮助。

假设一个 Sqlalchemy 模型 Account 具有 PK 列 id 并且路由定义如下:

accounts = Blueprint('accounts', __name__)
@accounts.route("/accounts/<int:account_id>")
def detail(self, account_id):

    _account = Account.get_or_404(account_id)
    # show account details
    return render('account-details.html', account=_account)

按如下方式定义您的帐户 link 列:

account = LinkCol(
    name='Account #',
    attr='id',
    endpoint='accounts.detail',
    url_kwargs=dict(account_id='id'),
)

参数含义如下:

name # the column header text
attr # the name of the attribute of the account object to render as the text in the <td> cell
endpoint # the endpoint to link to
url_kwargs # the arguments that get passed to the url_for function along with the endpoint

link 的构造就好像您为 table 中的每一行做了一个 url_for,如下所示:

_row_account_detail_url = url_for('accounts.detail', account_id=row_id)

示例 Table 定义:

class AccountTable(Table):

    account = LinkCol(
        name='Account #',
        attr='id',
        endpoint='accounts.detail',
        url_kwargs=dict(account_id='id'),
    )

    service_address = Col(
        'Service Address',
        attr='service_address'
    )

    #  other columns