其他操作按钮在flask-admin上不起作用 [英] Additional action button doesn't work on flask-admin

查看:278
本文介绍了其他操作按钮在flask-admin上不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试向flask-admin表单添加另一项操作.

I'm trying to add one more action to flask-admin forms.

它必须增加等级(+1),并且可以批量操作,但不能单次操作.请帮助我找到该错误,我花了很多时间尝试使此问题正常运行.

It has to increment rating (+1) and it works with batch action, but not with single. Please help me find the bug, I've spent a lot of time trying to make this thing work properly.

代码如下:

我在模板文件夹-custom_lists.html

I made an html template in templates folder - custom_lists.html

{% extends 'admin/model/list.html' %}
{% block list_row_actions %}
    {{ super() }}
  <form class="icon" method="POST" action="/admin/user/action/">
    <input id="action" name="action" value="approve" type="hidden">
    <input name="rowid" value="{{ get_pk_value(row) }}" type="hidden">
    <button onclick="return confirm('Are you sure you want to approve selected items?');" title="Approve">
      <span class="fa fa-ok glyphicon glyphicon-ok"></span>
    </button>
  </form>
{% endblock %}

此操作成功,并在列表上显示一个图标,但是如果我单击它,它会显示

this succeeded with an icon on the list, but if i click to it - it says

未找到

在服务器上找不到请求的URL.如果输入URL 手动,请检查您的拼写,然后重试.

The requested URL was not found on the server. If you entered the URL manually please check your spelling and try again.

添加到模板文件夹,并添加到AdidasView类,

added to templates folder and added to AdidasView class this:

list_template = 'custom_list.html'
@action('approve', 'Approve', 'Are you sure you want to approve selected items?')
def action_approve(self, ids):
    try:
        query = Adidas.query.filter(Adidas.id.in_(ids))

        count = 0
        for image in query.all():
            image.rating += 1
            count += 1
            db.session.commit()
        flash(ngettext('Item was successfully approved.',
                       '%s items were successfully approved.'%count,count))
    except Exception as ex:
        if not self.handle_view_exception(ex):
            raise

        flash(gettext('Failed to approve items. %(error)s', error=str(ex)), 'error')

推荐答案

我没有更改模板,但通过设置column_extra_row_actions变量并定义action_play函数,做了如下不同的操作

I have not changed the template but I have done it differently as following by setting the column_extra_row_actions variable and defining the action_play function

column_extra_row_actions = [
        EndpointLinkRowAction('glyphicon glyphicon-play', 'event.action_play')
    ]

@expose('/action/play', methods=('GET',))
def action_play(self, *args, **kwargs):
    return self.handle_action()

这篇关于其他操作按钮在flask-admin上不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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