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
V2EX  ›  Python

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

  •  
  •   Codelike · 2018-06-06 14:13:59 +08:00 · 3204 次点击
    这是一个创建于 2144 天前的主题,其中的信息可能已经有所发展或是发生改变。
    按照官方文档,在蓝图文件夹新建一个 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-06-06 15:11:38 +08:00
    py 文件,写 java 写顺手了( 0.0 )。
    12 条回复    2018-06-07 08:34:19 +08:00
    gnozix
        1
    gnozix  
       2018-06-06 14:33:12 +08:00
    .python 文件???没用过,不了解
    ranleng
        2
    ranleng  
       2018-06-06 14:34:56 +08:00
    .python 是啥?
    (而且这种情况应该上全部代码吧. 就三行 没上下文怎么知道哪出错了..
    Mrkon
        3
    Mrkon  
       2018-06-06 14:39:09 +08:00
    文件名是 errors.py,还是 errors.python。正确的为 errors.py
    你的程序应该是没有错的
    jhacker
        4
    jhacker  
       2018-06-06 14:46:31 +08:00
    from . import errors 是不是写在文件最后的面?
    Codelike
        5
    Codelike  
    OP
       2018-06-06 15:14:57 +08:00
    @Mrkon 是 py 文件。我试了一下。在根目录的__init__.py ,写上
    @app.errorhandler(404)
    def page_not_found(e):
    return render_template('404.html'),404
    是可以正常显示自定义错误界面的。
    Codelike
        6
    Codelike  
    OP
       2018-06-06 15:15:08 +08:00
    @jhacker 是的
    jhacker
        7
    jhacker  
       2018-06-06 15:36:18 +08:00
    最好上传下项目,不然光你这样说没法定位问题
    jhacker
        8
    jhacker  
       2018-06-06 16:02:39 +08:00
    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-06-06 16:05:46 +08:00
    在__init__.py 中创建了蓝本吗? main = Blueprint('main', __name__)
    Codelike
        10
    Codelike  
    OP
       2018-06-06 22:01:53 +08:00
    @jhacker 晚上又运行了一次,就正常运行了。一联萌币。谢谢了。
    jhacker
        11
    jhacker  
       2018-06-07 08:32:21 +08:00
    @Codelike
    昨天也有点懵, 其实 app_errorhandler 是对于蓝图起作用的,errorhandler 是对于全局的异常处理
    jhacker
        12
    jhacker  
       2018-06-07 08:34:19 +08:00
    哎呀又看错了,app_errorhandler 是全局的
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1023 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 36ms · UTC 19:23 · PVG 03:23 · LAX 12:23 · JFK 15:23
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.