Added a pattern for custom error pages.
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Armin Ronacher · committed by Armin Ronacher · parent ea5e654 · Browse files · 3 files·+80 added−0 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+80 added−0 removed
- other:+80 added−0 removed
Provenance
- Commit
267f5f5- Tree
62d39f4- Author
Armin Ronacher <armin.ronacher@active-4.com>- Committer
Armin Ronacher <armin.ronacher@active-4.com>- Parent
ea5e654
3 files changed+80 added−0 removed
added:docs/patterns/errorpages.rst+77 added
1+Custom Error Pages2+==================3+4+Flask comes with a handy :func:`~flask.abort` function that aborts a5+request with an HTTP error code early. It will also provide a plain black6+and white error page for you with a basic description, but nothing fancy.7+8+Depening on the error code it is less or more likely for the user to9+actually see such an error.10+11+Common Error Codes12+------------------13+14+The following error codes are some that are often displayed to the user,15+even if the application behaves correctly:16+17+*404 Not Found*18+ The good old "chap, you made a mistake typing that URL" message. So19+ common that even novices to the internet know that 404 means: damn,20+ the thing I was looking for is not there. It's a very good idea to21+ make sure there is actually something useful on a 404 page, at least a22+ link back to the index.23+24+*403 Forbidden*25+ If you have some kind of access control on your website, you will have26+ to send a 403 code for disallowed resources. So make sure the user27+ is not lost when he tries to access a resource he cannot access.28+29+*410 Gone*30+ Did you know that there the "404 Not Found" has a brother named "41031+ Gone"? Few people actually implement that, but the idea is that32+ resources that previously existed and got deleted answer with 41033+ instead of 404. If you are not deleting documents permanently from34+ the database but just mark them as deleted, do the user a favour and35+ use the 410 code instead and display a message that what he was36+ looking for was deleted for all ethernity.37+38+*500 Internal Server Error*39+ Usually happens on programming errors or if the server is overloaded.40+ A terrible good idea to have a nice page there, because your41+ application *will* fail sooner or later (see also:42+ :ref:`application-errors`).43+44+45+Error Handlers46+--------------47+48+An error handler is a function, just like a view function, but it is49+called when an error happens and is passed that error. The error is most50+likely a :exc:`~werkzeug.exceptions.HTTPException`, but in one case it51+can be a different error: a handler for internal server errors will be52+passed other exception instances as well if they are uncought.53+54+An error handler is registered with the :meth:`~flask.Flask.errorhandler`55+decorator and the error code of the exception. Keep in mind that Flask56+will *not* set the error code for you, so make sure to also provide the57+HTTP status code when returning a response.58+59+Here an example implementation for a "404 Page Not Found" exception::60+61+ from flask import render_template62+63+ @app.errorhandler(404)64+ def page_not_found(e):65+ return render_template('404.html'), 40466+67+An example template might be this:68+69+.. sourcecode:: html+jinja70+71+ {% extends "layout.html" %}72+ {% block title %}Page Not Found{% endblock %}73+ {% block body %}74+ <h1>Page Not Found</h1>75+ <p>What you were looking for is just not there.76+ <p><a href="{{ url_for('index') }}">go somewhere nice</a>77+ {% endblock %}modified:docs/patterns/index.rst+1 added
25 unmodified lines26 wtforms27 templateinheritance28 flashing29 jquery30+ errorpagesmodified:docs/errorhandling.rst+2 added
1+.. _application-errors:2+3 Handling Application Errors4 ===========================5 6 .. versionadded:: 0.5228 unmodified lines