自定义烧瓶管理员行操作 POST方法 GET方法 Glyphicons

问题描述

我要在flask管理模板中添加带有删除和编辑图标的另一个按钮,并希望将某行的该行数据作为发布请求发送。我知道我必须在admin / model / list.html模板中进行编辑。但我没有得到如何添加功能。 请帮忙。 预先感谢。

this is current default view for admin page

解决方法

您需要为视图定义自定义操作按钮。在Flask-Admin教程中没有描述此过程,但在API description中提到了此过程。

POST方法

如果您需要为POST方法创建按钮,则应实现一个类似delete_row的jinja2宏。可能看起来像这样(我将文件命名为“ 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方法

为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"),methods=("GET",))
    def copy_view(self):
        """The method you need to call"""

Glyphicons

Glyphicons是图标库,与Flask-Admin使用的Bootstrap v3库捆绑在一起。如果您在Flask-Admin初始化中选择了以下Bootstrap版本,则可以使用它:

from flask_admin import Admin

admin = Admin(template_mode="bootstrap3")

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