我正在尝试向 Flask-admin 表单添加一项操作。
它必须增加评级(+1)并且它适用于批量操作,但不适用于单个操作。请帮我找到这个错误,我花了很多时间试图让这个东西正常工作。
这是代码:
我在 templates 文件夹中制作了一个 html 模板 - 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 %}
这成功了,列表上有一个图标,但如果我点击它 - 它说
未找到
在服务器上找不到请求的 URL。如果您输入了网址
请手动检查拼写并重试。
添加到 templates 文件夹并添加到 AdidasView 类:
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')