Create Flask HTTP error handlers that use template rendering.
Project description
flask-error-templating
Create Flask HTTP error handlers that use template rendering. This is a very small and simple idea but I couldn't find anything like it so I made it myself.
Installation
Install with pip install flask-error-templating
Usage
create_http_error_handlers(app, error_pages, page_template_file)
app
is a handle to your Flask
object.
error_pages
is a list of ErrorPage
objects. It accepts three arguments: error_code
, message
and long_message
. error_code
and message
are required; long_message
is optional and if it is not present then it will not be rendered into the template. Note that it's possible to have some ErrorPage
objects with long_message
set and others without
page_template_file
is the filename of a HTML file in your projects templates
folder. Parameters supplied to the file for template rendering are error_code
, message
and long_message
. See the above paragraph for information on these parameters. If long_message
is not present then an empty string will be rendered in its place - this allows the same template to serve pages with long message and also without.
Complete basic example:
from flask import *
from flask_error_templating import ErrorPage, create_http_error_handlers
app = Flask(__name__)
@app.route('/')
def homepage():
return '<h1>Homepage</h1>'
error_pages = [
ErrorPage(400, 'Bad request'),
ErrorPage(400, 'Access is denied to this page.'),
ErrorPage(403, 'You are forbidden to view this page.',
'A very long message that we also want to display in the long_message field'),
ErrorPage(404, 'The page you are looking for does not exist'),
ErrorPage(418, 'I\'m a teapot!')
]
create_http_error_handlers(app, error_pages, 'http_error.html')
if __name__ == '__main__':
app.run()
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for flask-error-templating-0.0.2.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1efa76693e2e0814880ff74f1412e4fae4bb35f41eef5519ac71128b5dab76fe |
|
MD5 | 5b42474de12182768b30c0f73630fa6c |
|
BLAKE2b-256 | 7210ffddf18dbc04adb3cafab573c698adf53a4378a7c78acbb0d5191343fb86 |
Hashes for flask_error_templating-0.0.2-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 904381e13e7ac79b43a91604c5c9776be19d6c6f96f5105944febd9dd476712c |
|
MD5 | 3f26c86503451a0d0fe3313ad041f9a1 |
|
BLAKE2b-256 | 797fa07714f8c81a84c6142418dbe84fca8cf0978703ac2887d89aa021f4d0a4 |