首页 > 解决方案 > 在flask-admin中添加要查看的路线

问题描述

所以基本上我正在尝试向我的 Flask + Flask-Admin 应用程序添加一个自定义端点,该应用程序允许管理员下载 CSV。

像这样的东西:

class MyAdminIndexView(AdminIndexView):
    @expose('/')
    def index(self):
        return self.render('admin/home.html')

    def is_accessible(self):
        return current_user.has_role('admin')

    @expose('/csv-export')
    def csv_export(self):
        batch_num = request.args.get('batch_num')

        if not batch_num:
            flash('Invalid batch id', 'danger')
            abort(404)

        si = io.StringIO()
        cw = csv.writer(si)

        rows = MyObj.query.filter_by(batch_num=batch_num).all()

        cw.writerows(rows)
        output = make_response(si.getvalue())
        output.headers["Content-Disposition"] = "attachment; filename=export.csv"
        output.headers["Content-type"] = "text/csv"
        return output

但是,如果我将此添加到我的基本管理视图中,“/csv-export”路由不会注册,我会得到 404。

我想我可以为这个端点添加一个全新的蓝图,但对于一个不需要单独模板视图的简单端点来说似乎需要做很多工作。有小费吗?

标签: flaskflask-admin

解决方案


推荐阅读