Python 自定义flask管理行操作
我想在flask管理模板中添加另一个带有删除和编辑图标的按钮,并希望将某些路由的行数据作为post请求发送。我知道我必须在admin/model/list.html模板中进行编辑。但我不知道如何添加这些功能。 请帮忙。 提前谢谢Python 自定义flask管理行操作,python,flask,flask-sqlalchemy,flask-login,flask-admin,Python,Flask,Flask Sqlalchemy,Flask Login,Flask Admin,我想在flask管理模板中添加另一个带有删除和编辑图标的按钮,并希望将某些路由的行数据作为post请求发送。我知道我必须在admin/model/list.html模板中进行编辑。但我不知道如何添加这些功能。 请帮忙。 提前谢谢 您需要为视图定义自定义操作按钮。Flask Admin教程中未描述此过程,但在中提到了此过程 POST方法 如果您需要为POST方法创建一个按钮,您应该实现一个像这样的jinja2宏。它可能看起来像这样,我将文件命名为custom_row_actions.html: {
您需要为视图定义自定义操作按钮。Flask Admin教程中未描述此过程,但在中提到了此过程 POST方法 如果您需要为POST方法创建一个按钮,您应该实现一个像这样的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 %}
之后,您必须在视图中进行一些更改:
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"""
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"""
获取方法
为GET方法创建按钮要简单得多。您不需要覆盖模板,只需向视图中添加操作即可:
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"""
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管理员使用的引导v3库捆绑在一起。如果在Flask Admin初始化时选择此引导版本,则可以使用它:
from flask_admin import Admin
admin = Admin(template_mode="bootstrap3")
您可以查看中的可用图标。您需要为视图定义自定义操作按钮。Flask Admin教程中未描述此过程,但在中提到了此过程 POST方法 如果您需要为POST方法创建一个按钮,您应该实现一个像这样的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 %}
之后,您必须在视图中进行一些更改:
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"""
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"""
获取方法
为GET方法创建按钮要简单得多。您不需要覆盖模板,只需向视图中添加操作即可:
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"""
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管理员使用的引导v3库捆绑在一起。如果在Flask Admin初始化时选择此引导版本,则可以使用它:
from flask_admin import Admin
admin = Admin(template_mode="bootstrap3")
您可以在中查看可用的图标