【问题标题】:Adding custom HTTP error and exceptions to flask向烧瓶添加自定义 HTTP 错误和异常
【发布时间】:2021-01-13 01:50:41
【问题描述】:

如何添加自定义 HTTP 错误?据我了解,6XX 和 7XX 类中的错误代码可用于此目的,但 Flask 不允许我处理这些错误,因为它说 this(601) 不是公认的 HTTP 错误。

编辑: 这是正确的代码:-

from werkzeug.exceptions import HTTPException

@errors.errorhandler(HTTPException)
def error_601(HTTPException):
    return render_template('errors/601.html'), 601


class No_results_found(HTTPException):
    code = 601
    description = '<p>No_results_found.</p>'

errors.register_error_handler(No_results_found, error_601)

【问题讨论】:

    标签: http flask werkzeug


    【解决方案1】:

    请在error_601之前添加@app.errorhandler(HTTPException)

    文档:https://flask.palletsprojects.com/en/1.1.x/errorhandling/#registering

    【讨论】:

    • 感谢您的提示,我错过了文档中的装饰器。
    猜你喜欢
    • 1970-01-01
    • 2014-11-13
    • 1970-01-01
    • 2017-05-11
    • 1970-01-01
    • 2021-05-20
    • 2015-01-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多