2

I have the following structure for my flask application. When an invalid url comes in a 404 error is called, but my 404.html requires data from the context_processor but in the abort 404 the blueprint is None so context_processor is never called and 404.html is missing data.

How can I approach this differently?

init.py

def create_app(settings_overide=None):
    app = factory.create_app(__name__, __path__, settings_overide)
    if not app.debug:
        app.errorhandler(404)(page_not_found)

    return app

def page_not_found(e):
    return render_template('404.html'), 404

processors.py

@blueprint.context_processor
def load_global_data():
    return get_data()
lennard
  • 523
  • 6
  • 19

1 Answers1

1

When a route is not found, no blueprint is set yet (because setting a blueprint requires a route to be determined first). As such a 404 error handler can't count on a blueprint to have been determined.

You'd have to execute the get_data() call manually. Test if a specific global has been set, then execute the get_data() function if not before rendering your 404 template.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343