My Flask App works great on local loopback with the Flask server, but now I am trying to get apache2 to serve the application on my private 192.168.0.0/24 network (my server’s IP is 192.168.0.18).ĮDIT3: I have followed most of Mr. convert to AssertionError if desirable.How to properly serve Flask App on Ubuntu via Apache 2 and mod_wsgi? 404 Not Found Error
Feel free to treat it as a failed assertion about application’s internal stateĪnd e.g. FATAL_ERROR_RESPONSE = ( "Internal Server Error", 500 )Ĭatching TemplateNotFound exception is admittedly a little paranoid here, as it can occur pretty much exclusivelyĭue to a programming error elsewhere. fatal ( "Missing template for HTTP error page: %s ", template ) return FATAL_ERROR_RESPONSE #: Response emitted when an error occurs in the error handler itself.
html' % code ) try : return render_template ( str ( template )), code except TemplateNotFound : # shouldn't happen if the handler has been wired up properly app. debug ( "HTTP error %s, rendering error page", code ) template = ERRORS_DIR / ( ' %s. warning ( "Got spurious argument to HTTP error handler: %r ", error ) return FATAL_ERROR_RESPONSE app. :return: HTTP response to be returned """ code = getattr ( error, 'code', None ) if not code : app. :param error: :class:`~` representing the HTTP error. To help distinguishing between the different status codes,įlask by default invokes our error handlers with anįrom flask import render_template from jinja2 import TemplateNotFound def error_handler ( error ): """Universal handler for all HTTP errors.
One function to handle them allĪlthough I’ve used a plural here, in reality we only need a single handler, as it’ll be perfectly capable ofĭealing with any HTTP error we bind it to. Once we have them, we can try wiring up the handlers programmatically and making them do the right thing. warning ( "Found error template for non-error HTTP status %s ", code ) continue yield code 404.html except ValueError : pass # could be some base.html template, or similar else : if code < 400 : app. is_file ()) for template in potential_error_templates : try : code = int ( template. template_folder, ERRORS_DIR ) potential_error_templates = ( entry for entry in error_templates_dir. ERRORS_DIR = PurePath ( 'errors' ) def get_supported_error_codes (): """Returns an iterable of HTTP status codes for which we have a custom error page templates defined. 404.html), getting all those codes is quite simple 1:įrom pathlib import Path, PurePath from mywebapp import app #: Path to the directory where HTML templates for error pages are stored. Revolves around predefined templates, let’s just define the handlers automatically based on the HTML files themselves.Īssuming we store them in the same template directory - say, errors/ - and name their files after Ideally, we would like to avoid writing any Python code at all for each individual error handler.
Which is, of course, why we’d want to deal with all in a little smarter way and with less boilerplate. Pick a corresponding template and simply render it. You’ll notice they get pretty repetitive: for each erroneous HTTP status code (404, 403, 400, etc.), If you have more handlers like that, though, Request error than to send back some HTML with an appropriate message. There’s rarely anything else to do in response to serious ( 404 ) def not_found ( error ): return render_template ( 'errors/404.html' )Īlbeit simple, the above example is actually very realistic.