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)
Parameters
app
app
is a handle to your Flask
object. Need I write more?
error_pages
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.
Example of error_pages
:
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!')
]
page_template_file
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.
Example of page_template_file
:
<!DOCTYPE html>
<html>
<body>
<h1>{{ error_code }}</h1>
<h2>{{ message }}</h2>
<br>
<p>{{ long_message }}</p>
</body>
</html>
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-1.0.0.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | bc8d360cc25d526d6f2260ca9ebfc1414bdf9de80cb80c7a0791b1eec7673859 |
|
MD5 | 1835e1ad37551f300de7e5d70b8edcd0 |
|
BLAKE2b-256 | e34d371bb9af6efd0fe05589fa20c1f9fd767ef456df0f74fd604441c62e6ca3 |
Hashes for flask_error_templating-1.0.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 424b1f5e84f49746f2006bb5fdb10e7b39aaa129370481e909ca3c100196219b |
|
MD5 | b78fd3800277d75913d379e158cc3dc4 |
|
BLAKE2b-256 | b4e1fa5b19a50701b1793791ad556dceffc80875eec7c23a90f0abc17035bb41 |