自定义烧瓶管理行操作 [英] Customizing the flask admin row actions

查看:22
本文介绍了自定义烧瓶管理行操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在烧瓶管理模板中添加另一个带有删除和编辑图标按钮,并希望将某些路由的行数据作为POST请求发送。我知道我必须在admin/model/list.html模板中编辑。但是我不明白如何添加这些功能。 请帮帮忙。 提前谢谢。

推荐答案

您需要为视图定义自定义操作按钮。此过程未在烧瓶管理教程中介绍,但在API description中有提及。

POST方法

如果需要为POST方法创建按钮,则应该实现如下delete_row操作的JJANA2宏。它可能如下所示(我将该文件命名为";custom_row_actions.html";):

{% macro copy_row(action, row_id, row) %}
<form class="icon" method="POST" action="{{ get_url('.copy_view') }}">
  <input type="hidden" name="row_id" value="{{ get_pk_value(row) }}"/>
  <button type="submit" title="{{ _gettext('Copy record') }}">
    <span class="glyphicon glyphicon-copy"></span>
  </button>
</form>
{% endmacro %}

然后为记录列表创建一个模板,并在其中导入宏库(我将其命名为";my_list.html";):

{% extends 'admin/model/list.html' %}
{% import 'custom_row_actions.html' as custom_row_actions with context %}

之后,您必须在视图中进行几处更改:

from flask_admin import expose
from flask_admin.contrib.sqla.view import ModelView
from flask_admin.model.template import TemplateLinkRowAction

class MyView(ModelView):
    list_template = "my_list.html"  # Override the default template
    column_extra_row_actions = [  # Add a new action button
        TemplateLinkRowAction("custom_row_actions.copy_row", "Copy Record"),
    ]

    @expose("/copy", methods=("POST",))
    def copy_view(self):
        """The method you need to call"""

获取方法

为GET方法创建按钮要简单得多。您不需要覆盖模板,只需向视图添加操作:

from flask_admin import expose
from flask_admin.contrib.sqla.view import ModelView
from flask_admin.model.template import EndpointLinkRowAction

class MyView(ModelView):
    column_extra_row_actions = [  # Add a new action button
        EndpointLinkRowAction("glyphicon glyphicon-copy", ".copy_view"),
    ]

    @expose("/copy", methods=("GET",))
    def copy_view(self):
        """The method you need to call"""

英文图标

Glyphicons是与Flask-Admin使用的Bootstrap v3库捆绑在一起的图标库。如果您在Flask-Admin Initialization上选择此引导程序版本,则可以使用它:

from flask_admin import Admin

admin = Admin(template_mode="bootstrap3")

您可以在Bootsrap v3 documentation中查看可用的图标。

这篇关于自定义烧瓶管理行操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆