Register_blueprint checkbox error (Python)

I have a project on Flask. And when I include Blueprint in my Flask application, I have errors. Code from Miguel Greenberg's book on Flask.

Project Tree:

. β”œβ”€β”€ app β”‚  β”œβ”€β”€ __init__.py β”‚  β”œβ”€β”€ main β”‚  β”‚  β”œβ”€β”€ errors.py β”‚  β”‚  β”œβ”€β”€ __init__.py β”‚  β”‚  └── views.py β”‚  β”œβ”€β”€ static β”‚  β”‚  β”œβ”€β”€ static_files β”‚  └── templates β”‚  └── html_files β”œβ”€β”€ config.py β”œβ”€β”€ manage.py 

create_app () in the application / __ init __. py

 def create_app(config_name): app = Flask(__name__) app.config.from_object(config[config_name]) config[config_name].init_app(app) bootstrap.init_app(app) db.init_app(app) from .main import main as main_blueprint app.register_blueprint(main_blueprint) return app 

enumeration app / main / __ init __. py

 from flask import Blueprint main = Blueprint('main', __name__) from . import views, errors 

Process output line to line

 Traceback (most recent call last): File "/opt/passenger/passenger-4.0.57/helper-scripts/wsgi-loader.py", line 320, in <module> app_module = load_app() File "/opt/passenger/passenger-4.0.57/helper-scripts/wsgi-loader.py", line 61, in load_app return imp.load_source('passenger_wsgi', startup_file) File "/home/m/mallts/dev.wget-studio.ru/myenv/lib/python3.4/imp.py", line 171, in load_source module = methods.load() File "<frozen importlib._bootstrap>", line 1220, in load File "<frozen importlib._bootstrap>", line 1200, in _load_unlocked File "<frozen importlib._bootstrap>", line 1129, in _exec File "<frozen importlib._bootstrap>", line 1471, in exec_module File "<frozen importlib._bootstrap>", line 321, in _call_with_frames_removed File "passenger_wsgi.py", line 16, in <module> from manage import app as application File "/home/m/mallts/dev.wget-studio.ru/fikls/manage.py", line 7, in <module> app = create_app('default') #(os.getenv('FLASK_CONFIG') or 'default') File "/home/m/mallts/dev.wget-studio.ru/fikls/app/__init__.py", line 19, in create_app app.register_blueprint(main_blueprint) File "/home/m/mallts/dev.wget-studio.ru/myenv/lib/python3.4/site-packages/flask/app.py", line 62, in wrapper_func return f(self, *args, **kwargs) File "/home/m/mallts/dev.wget-studio.ru/myenv/lib/python3.4/site-packages/flask/app.py", line 889, in register_blueprint blueprint.register(self, options, first_registration) File "/home/m/mallts/dev.wget-studio.ru/myenv/lib/python3.4/site-packages/flask/blueprints.py", line 153, in register deferred(state) File "/home/m/mallts/dev.wget-studio.ru/myenv/lib/python3.4/site-packages/flask/blueprints.py", line 128, in wrapper func(state) File "/home/m/mallts/dev.wget-studio.ru/myenv/lib/python3.4/site-packages/flask/blueprints.py", line 399, in <lambda> self.name, code_or_exception, f)) File "/home/m/mallts/dev.wget-studio.ru/myenv/lib/python3.4/site-packages/flask/app.py", line 62, in wrapper_func return f(self, *args, **kwargs) File "/home/m/mallts/dev.wget-studio.ru/myenv/lib/python3.4/site-packages/flask/app.py", line 1090, in _register_error_handler 'It is currently not possible to register a 500 internal ' \ AssertionError: It is currently not possible to register a 500 internal server error on a per-blueprint level. 

My application / main / errors.py

 from flask import render_template from . import main @main.errorhandler(404) def page_not_found(e): return render_template('404.html'), 404 @main.errorhandler(500) def internal_server_error(e): return render_template('500.html'), 500 

My app / main / views.py

 from flask import render_template, url_for, session, redirect from . import main @main.route('/') def index(): return render_template('index.html') 
+7
flask blueprint
source share
2 answers

You cannot create a handler 500 in a drawing.

https://www.bountysource.com/issues/1400879-can-not-register-a-500-error-handler-for-blueprint

You will need to do this in the main application. I feel your pain though, since you have a factory application and no routes in the / __ init __ application. Py .

I usually do something like this in the module of my factory application to fix this flaw:

 def register_errorhandlers(app): def render_error(error): error_code = getattr(error, 'code', 500) return render_template("{0}.html".format(error_code)), error_code for errcode in [500]: app.errorhandler(errcode)(render_error) return None 

and in the create_app (config_name) method, app factory, I call this:

 register_errorhandlers(app) 
+6
source share

I just ran into this problem with another application that I used refactoring for drawings. There is no need for a workaround, as Flask offers a decorator for this case: app_errorhandler . It works just like an errorhandler , because it logs the error route for the entire application, but works with drawings. For example:

 from flask import render_template from . import main @main.app_errorhandler(404) def page_not_found(e): return render_template('404.html'), 404 @main.app_errorhandler(500) def internal_server_error(e): return render_template('500.html'), 500 

Greenberg’s book, Flask Development β€” An Excellent Reading β€” Use this decorator for error pages registered in the main project. You can view the accompanying code here . First I skipped this.: P

+10
source share

All Articles