Additional action button doesn't work on flask

2019-07-22 20:44发布

问题:

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

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.

Here's the code:

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

Not Found

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

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')

回答1:

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()