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, **kwargs)
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>
keyword arguments
Often, you will want to pass things like the name of your app to the template when it is being rendered. To allow passing this value, all keyword arguments after page_template_file will be passed to Flask's render_template() function.
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', app_name='Some testing app')
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
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file flask-error-templating-1.1.1.tar.gz.
File metadata
- Download URL: flask-error-templating-1.1.1.tar.gz
- Upload date:
- Size: 3.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.2 importlib_metadata/4.6.1 pkginfo/1.7.1 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.0 CPython/3.9.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
9a907e00a5c55d9efb49e839ba31734f83605b7489456c766f8a5dffce061abc
|
|
| MD5 |
00a82a98694c5a81d0cb6598c05feb0e
|
|
| BLAKE2b-256 |
13be989789be1e6719d79d39cb9d5f6a22e5d1cdf91cd554300f8f901ec4bddf
|
File details
Details for the file flask_error_templating-1.1.1-py3-none-any.whl.
File metadata
- Download URL: flask_error_templating-1.1.1-py3-none-any.whl
- Upload date:
- Size: 4.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.2 importlib_metadata/4.6.1 pkginfo/1.7.1 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.0 CPython/3.9.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e6117da9b1636dbd3feb754cb8c3cda088425801e968030ee2f4f40b35a140af
|
|
| MD5 |
6b0f29d02b6c2ca8721bf09f8314544f
|
|
| BLAKE2b-256 |
f2e39e9f9cf1d132249b785c3eeca303829e5d6dc038f6223274c23f89804695
|