gpt4 book ai didi

python - flask admin - 将删除按钮移动到编辑 View

转载 作者:太空宇宙 更新时间:2023-11-04 02:45:38 25 4
gpt4 key购买 nike

我是 flask admin 的新手,我需要将删除按钮移动到编辑 View 。

这是我的其他 View 继承自的 AdminModelView 类。

class AdminModelView(sqla.ModelView):
can_view_details = True


# column_extra_row_actions = [ViewRowAction()]

def is_accessible(self):
if not current_user.is_active or not current_user.is_authenticated:
return False
if current_user.can('administrator'):
return True
return False

def _handle_view(self, name, **kwargs):
"""
Override builtin _handle_view in order to redirect users when a view is not accessible.
"""
if not self.is_accessible():
if current_user.is_authenticated:
# permission denied
abort(403)
else:
# login
return redirect(url_for('auth.login', next=request.url))
print(self._edit_form_class)


def get_list_row_actions(self):
"""
Return list of row action objects, each is instance of
:class:`~flask_admin.model.template.BaseListRowAction`
"""
actions = []

if self.can_view_details:
if self.details_modal:
actions.append(template.ViewPopupRowAction())
else:
actions.append(template.ViewRowAction())

return actions + (self.column_extra_row_actions or [])

我重新定义了 get_list_row_actions 以从 ListView 中删除编辑和删除按钮。我想知道我是否可以更改 AdminModelView 类的一部分,或者我是否需要更改编辑表单的模板。

最佳答案

我的解决方案:

class AdminModelView(sqla.ModelView) 上写了新端点:

@expose('/delete', methods=('DELETE',))
def delete_view(self):
"""
Delete model
"""
id = request.args.get('id')
if id is None:
return jsonify({"success": False}), 404
model = self.get_one(id)
db.session.delete(model)
db.session.commit()
flash("{0} deleted".format(model))
return jsonify({"success": True}), 200

使用这些宏来呈现删除按钮和编辑 View 模板的模式。

{% if admin_view.can_delete and model %}
{{ add_modal_button(url='#', title='Delete', content='Delete', modal_window_id='delete_modal', btn_class='btn btn-danger') }}
{% endif %}

{% macro delete_modal() %}
<div class="modal fade" id="delete_modal" tabindex="-1" role="dialog">
<div class="modal-dialog" role="document">
{# bootstrap version > 3.1.0 required for this to work #}
<div class="modal-content">
<div class="panel panel-danger">
<div class="panel-heading">Delete Record</div>
<div class="panel-body">
<p>You are about to delete this record. This action cannot be undone.</p>
<p>Would you like to proceed?</p>
</div>
<div class="panel-footer text-center">
<button type="button" class="btn btn-secondary" id="cancel_delete">Cancel</button>
<button type="button" class="btn btn-danger" id="confirm_delete">Delete</button>
</div>
</div>
</div>
</div>
</div>
{% endmacro %}

混合 Jinja2 和 JavaScript。这应该重构。 {{model.id}} 可以存储在 DOM 中,并在发出 AJAX 请求之前使用 jQuery 检索。

$("#cancel_delete").click(function() {
$("#delete_modal").modal("toggle")
});

$("#confirm_delete").click(function() {
$.ajax({
url: "../delete?id={{ model.id }}",
method: "DELETE"
}).done(function() {
window.location.replace("{{ return_url }}");
}).fail(function() {
$(".actions-nav").before(
'<div class="alert alert-danger alert-dismissable fade in">' +
'<a href="#" class="close" data-dismiss="alert" aria-label="close">&times;</a>' +
'<strong>Error:</strong> This object failed to delete.' +
'</div>'
)
})
})

关于python - flask admin - 将删除按钮移动到编辑 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45221274/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com