Additional action button doesn't work on flask-admin

扶醉桌前 提交于 2019-12-12 09:55:35

问题


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


来源:https://stackoverflow.com/questions/52037226/additional-action-button-doesnt-work-on-flask-admin

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!