【发布时间】: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