ssh*_*krv 2 python jinja2 flask flask-sqlalchemy flask-admin
我正在尝试向flask-admin表单添加其他操作。
它必须增加额定值(+1),并且可以批量操作,但不能单次使用。请帮助我找到错误,我花了很多时间尝试使此事情正常运行。
这是代码:
我在模板文件夹中创建了一个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 %}
Run Code Online (Sandbox Code Playgroud)
这成功地在列表上显示了一个图标,但是如果我单击它,它会说
未找到
在服务器上找不到请求的URL。如果您手动输入网址,请检查拼写,然后重试。
添加到模板文件夹,并添加到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')
Run Code Online (Sandbox Code Playgroud)
小智 6
我没有更改模板,但通过设置column_extra_row_actions变量并定义action_play函数,做了如下不同的处理
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()
Run Code Online (Sandbox Code Playgroud)