我正在使用flask,并且按功能有我的软件包,并且正在使用蓝图,这很好用,但是我希望有一个全局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