Flask register_blueprint error (Python)
Question: Sorry for my English, but.. I need help.

I have a project on Flask. And when I include Blueprint on my Flask app I have
errors. The code is from a book, Miguel Grinberg about 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 **app/__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
    

listing **app/main/__init__.py**

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

Passenger row proccess output

    
    
    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 **app/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')
    

Thx for your help


Answer: I just ran into this same issue with another application that I was
refactoring to use blueprints. There's no need for a workaround, since Flask
offers a decorator for just such a case:
[`app_errorhandler`](http://flask.pocoo.org/docs/0.10/api/#flask.Blueprint.app_errorhandler).
It works exactly like `errorhandler` in that it registers an error route for
the entire app, but it works with blueprints. Like so:

    
    
    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
    

Grinberg's book, _Flask Web Development_ \-- an excellent read -- uses this
decorator for error pages registered on the `main` blueprint. You can review
the companion code
[here](https://github.com/miguelgrinberg/flasky/blob/master/app/main/errors.py).
I missed it at first, too. :P

