python - 我尝试在 Flask 中使用 Blueprints 进行 404 错误处理,但似乎无法正常工作。这是我的代码:
问题描述
错误/handlers.py
from flask import render_template
from autoapp import app
from dockblaster.errors import errors_blueprint
@app.errorhandler(404)
@errors_blueprint.app_errorhandler(404)
def not_found_error(error):
return render_template('error_pages/page_not_found.html'), 404
错误/__init__.py:
from flask import Blueprint
errors_blueprint = Blueprint('errors', __name__)
import dockblaster.errors
我终于在 app.py 中注册了蓝图:
def create_app(config_object=ProdConfig):
"""An application factory, as explained here: http://flask.pocoo.org/docs/patterns/appfactories/.
:param config_object: The configuration object to use.
"""
app = Flask(__name__.split('.')[0])
app.config.from_object(config_object)
from dockblaster.errors import errors_blueprint
app.register_blueprint(errors_blueprint)
register_extensions(app)
register_blueprints(app)
return app
我似乎无法正常工作,因为我为重定向 404 错误创建的页面无法通过错误蓝图访问。
解决方案
我能够自己解决这个问题。
我这样做了:
我删除了 errors/handlers.py 文件,并将代码添加到 errors/ int .py 中,从而避免了另一个文件导入来访问蓝图处理程序的错误。这似乎对我有用。
推荐阅读
- python - 如何在python中将类对象存储在函数中
- elasticsearch - 将Kafka主题标题显示为Kibana中的字段,logstash add_field?
- django - 多对多关系的 Django 动态表单
- c# - 对所有可能的指标值、排列进行策略优化
- jquery - 在 Django 应用程序中单击时刷新显示的数据
- azure - 如何在 Azure 中检查向 IoT 中心发送消息的来源?
- c++ - 为什么虚拟 cudaMalloc 在moderngpu 中加速interval_gather?
- sql - JPA 查询参数 IN 或 IS NULL
- firebase - Firebase 实时数据库过滤数据
- python - PYTHON CSV 文件上传 - WebUI -> Lambda ->S3