flask 蓝图设置自定义错误页面,无法正常生效 - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
Codelike

flask 蓝图设置自定义错误页面,无法正常生效

  •  
  •   Codelike 2018 年 6 月 6 日 3933 次点击
    这是一个创建于 2879 天前的主题,其中的信息可能已经有所发展或是发生改变。
    按照官方文档,在蓝图文件夹新建一个 errors.python 文件。写上
    @main.app_errorhandler(404)
    def page_not_found(e):
    return render_template('404.html'),404
    在蓝图的 init.python 写上 from . import errors
    现在有几点疑问,1、按照我的想法,当写上 app_errorhandler,应该是用来自定义全局错误页面的,errorhandler 是用来自定义蓝图内部的错误页面的。2、运行过后,生成的 404 错误还是显示默认的 404 错误页面。app_errorhandler 和 errorhandler 都不生效。
    第 1 条附言    2018 年 6 月 6 日
    py 文件,写 java 写顺手了( 0.0 )。
    12 条回复    2018-06-07 08:34:19 +08:00
    gnozix
        1
    gnozix  
       2018 年 6 月 6 日
    .python 文件???没用过,不了解
    ranleng
        2
    ranleng  
       2018 年 6 月 6 日
    .python 是啥?
    (而且这种情况应该上全部代码吧. 就三行 没上下文怎么知道哪出错了..
    Mrkon
        3
    Mrkon  
       2018 年 6 月 6 日
    文件名是 errors.py,还是 errors.python。正确的为 errors.py
    你的程序应该是没有错的
    jhacker
        4
    jhacker  
       2018 年 6 月 6 日
    from . import errors 是不是写在文件最后的面?
    Codelike
        5
    Codelike  
    OP
       2018 年 6 月 6 日
    @Mrkon 是 py 文件。我试了一下。在根目录的__init__.py ,写上
    @app.errorhandler(404)
    def page_not_found(e):
    return render_template('404.html'),404
    是可以正常显示自定义错误界面的。
    Codelike
        6
    Codelike  
    OP
       2018 年 6 月 6 日
    @jhacker 是的
    jhacker
        7
    jhacker  
       2018 年 6 月 6 日
    最好上传下项目,不然光你这样说没法定位问题
    jhacker
        8
    jhacker  
       2018 年 6 月 6 日
    def _find_error_handler(self, e):
    """Return a registered error handler for an exception in this order:
    blueprint handler for a specific code, app handler for a specific code,
    blueprint handler for an exception class, app handler for an exception
    class, or ``None`` if a suitable handler is not found.
    """
    exc_class, code = self._get_exc_class_and_code(type(e))

    for name, c in (
    (request.blueprint, code), (None, code),
    (request.blueprint, None), (None, None)
    ):
    handler_map = self.error_handler_spec.setdefault(name, {}).get(c)


    看了下 Flask 相关的源码,貌似在 request.blueprint 这一步中本来是获取蓝图名称的,但是我调试后值一直为 None,也就是使用 app.errorhandler 进行装饰的函数
    Mrkon
        9
    Mrkon  
       2018 年 6 月 6 日
    在__init__.py 中创建了蓝本吗? main = Blueprint('main', __name__)
    Codelike
        10
    Codelike  
    OP
       2018 年 6 月 6 日
    @jhacker 晚上又运行了一次,就正常运行了。一联萌币。谢谢了。
    jhacker
        11
    jhacker  
       2018 年 6 月 7 日
    @Codelike
    昨天也有点懵, 其实 app_errorhandler 是对于蓝图起作用的,errorhandler 是对于全局的异常处理
    jhacker
        12
    jhacker  
       2018 年 6 月 7 日
    哎呀又看错了,app_errorhandler 是全局的
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     3059 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 34ms UTC 06:00 PVG 14:00 LAX 23:00 JFK 02:00
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86