【问题标题】:Python Flask - Error Handling with blueprintsPython Flask - 使用蓝图处理错误
【发布时间】:2019-11-06 10:48:17
【问题描述】:

我正在使用烧瓶,我有我的功能包,并且正在使用蓝图,这很好用,但我希望有一个全局 404 和错误页面,它位于任何特定功能包之外。

当我触发 404 烧瓶时,仍然使用默认的 404 处理程序处理此问题,但我没有得到我的自定义模板。以下是我的代码:

初始化.py

# Define the WSGI application object
app = Flask(__name__)

# Load Configuration
app.config.from_object('config')

from .user.route import mod_user as user_module
from .route.error_handler import mod_error as error_module

# Register blueprints
app.register_blueprint(user_module)
app.register_blueprint(error_module)

error_handler.py

import traceback

from flask import Flask, render_template, Blueprint

mod_error = Blueprint('error', __name__, template_folder='templates')


@mod_error.errorhandler(404)
def not_found(e):
    print(e)
    return render_template('404.html')


@mod_error.errorhandler(Exception)
def general_error(e):
    print(e)
    print(traceback.format_exc())
    return render_template('error.html')

我的特征路由定义在project.user.route.py

全局路由\错误处理程序位于project.route.error_handler.py

全局错误模板在project.templates

【问题讨论】:

    标签: python-3.x flask


    【解决方案1】:

    我设法解决了这个问题,这非常简单,当我从一个脚本中的所有内容转移到使用蓝图并创建错误处理模块时,我认为我需要在注释中使用模块名称:

    @mod_error.errorhandler(404)

    这样做的原因是因为这是我在控制器中为我的用户功能所做的:

    @mod_user.route('/read', methods=['POST', 'GET'])

    以下是我需要做的,即导入我的应用程序对象,然后将其用于错误处理函数:

    import traceback
    
    from flask import Flask, render_template, Blueprint
    from .. import app
    
    mod_error = Blueprint('error', __name__, template_folder='templates')
    
    
    @app.errorhandler(404)
    def not_found(e):
        print(e)
        return render_template('404.html')
    
    
    @app.errorhandler(Exception)
    def general_exception(e):
        print(e)
        print(traceback.format_exc())
        return render_template('error.html')
    

    这现在可以处理我的任何功能包之外的全局级别的所有错误,我想实现ControllerAdvice 的效果,它可以处理 Spring MVC 中所有控制器的所有异常。

    【讨论】:

      【解决方案2】:

      您实际上不必像@berimbolo 所说的那样导入app 对象。是的,他确实有正确的想法,因为在flask中,对于404和其他路由错误,docs说:

      蓝图无法处理 404 路由错误,因为 404 发生在路由级别,然后才能确定蓝图。

      因此,您无法在蓝图上使用 errorhandler() 处理 404 错误。这不适用于 404 错误:

      from flask import Flask, render_template, Blueprint
      mod_error = Blueprint('error', __name__, template_folder='templates')
      
      @mod_error.errorhandler(404)
      def not_found(e):
          ...
      

      相反,您必须将错误处理程序添加到应用程序对象中,就像 @berimbolo 所做的那样:

      @app.errorhandler(404)
      def not_found(e):
          print(e)
          return render_template('404.html')
      

      但是,这个问题是您增加了更改,您会遇到循环导入问题(this 之类的问题)。因为声明app对象的模块很可能会导入声明blueprint对象的模块,同时blueprint模块再导入app模块得到app对象.

      解决方案:就是使用app_errorhandlermethod on the blueprint对象。它的文档字符串说:

      就像 :meth:Flask.errorhandler 一样,只是为了一个蓝图。此处理程序用于所有请求,即使在蓝图之外。

      这意味着您不必执行循环导入操作。相反,你可以写:

      import traceback
      from flask import Flask, render_template, Blueprint
      
      mod_error = Blueprint('error', __name__, template_folder='templates')
      
      @mod_error.app_errorhandler(404)
      def not_found(e):
          print(e)
          return render_template('404.html')
      
      @app.app_errorhandler(Exception)
      def general_exception(e):
          print(e)
          print(traceback.format_exc())
          return render_template('error.html')
      

      【讨论】:

        猜你喜欢
        • 2019-06-20
        • 1970-01-01
        • 2021-05-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-09-16
        • 1970-01-01
        • 2012-09-14
        相关资源
        最近更新 更多