flask - 在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。
我想我可以为这个端点添加一个全新的蓝图,但对于一个不需要单独模板视图的简单端点来说似乎需要做很多工作。有小费吗?
解决方案
推荐阅读
- python - 在 tkinter 中更改仅 askopenfilename 的图标,同时在 python 中隐藏主 Tk() 窗口
- java - 由 Spring-Boot 端点调用时,Java Jersey 端点获取空参数
- python - Kafka 获取对应消息 ID 的分区 ID
- java - 如何从自定义远程存储库添加库
- visual-studio - 命令行参数不会传递给 Docker 容器入口点
- python - 比较python中的两个正在运行的脚本
- node.js - 错误 cb.apply 不是 Node Express 项目中 [尝试安装软件包时] 的功能 --
- django - 如何在不从 Django 中的中间件引发异常的情况下向用户报告错误
- swiper - 使用缩略图和进度创建 swiper
- java - spring.config.import 和 @ConfigurationProperties