我们可以在单独的模块中有Flask错误处理程序吗 [英] Can we have Flask error handlers in separate module

查看:36
本文介绍了我们可以在单独的模块中有Flask错误处理程序吗的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在将Flask应用程序从基于函数的视图迁移到可插入的视图,除了错误处理程序之外,它们均按预期工作.我试图将所有错误处理程序放在一个名为error_handlers.py的模块中,并将其导入到主模块中.但这是行不通的.尝试以相同的方式在Google中搜索并找到一些Git仓库,但是它不适用于我,请帮助我解决此问题.

We are migrating our Flask app from function based views to pluggable views, all working as expected, except the error handlers. I am trying to put all the error handlers in a single module called error_handlers.py and importing it in to the main module. But it is not working. Tried searching in Google and find some Git repos following the same way, but it is not working for me, please help me to solve this issue.

app
 |
 |__ __init__.py
 |__ routing.py (which has the app=Flask(__name__) and imported error handlers here [import error_handlers])   
 |__ views.py 
 |__ error_handlers.py (Ex: @app.errorhandler(FormDataException)def form_error(error):return jsonify({'error': error.get_message()})
 |__ API (api modules)
      |
      |
      |__ __init__.py
      |__ user.py  

我正在使用Python 2.6和Flask 0.10.1.

I am using Python 2.6 and Flask 0.10.1.

推荐答案

我认为您像

routing.py 一样在 routing.py 中导入 error_handlers 模块.

I think you import error_handlers module in routing.py as like

import error_handlers

您最好像在 routing.py

from error_handlers import *

可能会对您有帮助:-)

It could be help you:-)

这篇关于我们可以在单独的模块中有Flask错误处理程序吗的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆