errorpages.rst
This file
Path
docs/patterns/errorpages.rst
79 lines · 3.0 KiB
Last changed by
Merge pull request #1191 from nluchs/patch-2Markus Unterwaditzerbefore adoptiona18dedc
History of this fileTake it
The walk stopped after 200 commits: lines older than 5d20501 point at it.
Fixed a reference in the docs.Armin Ronacherbefore adoption8a46eec
lines 1–2why, one step back1.. _errorpages:2Merge pull request #863 from jet-sun/patch-1Daniel Neuhäuserbefore adoption5d20501
lines 3–41why, one step back3Custom Error Pages4==================56Flask comes with a handy :func:`~flask.abort` function that aborts a7request with an HTTP error code early. It will also provide a plain black8and white error page for you with a basic description, but nothing fancy.910Depending on the error code it is less or more likely for the user to11actually see such an error.1213Common Error Codes14------------------1516The following error codes are some that are often displayed to the user,17even if the application behaves correctly:1819*404 Not Found*20 The good old "chap, you made a mistake typing that URL" message. So21 common that even novices to the internet know that 404 means: damn,22 the thing I was looking for is not there. It's a very good idea to23 make sure there is actually something useful on a 404 page, at least a24 link back to the index.2526*403 Forbidden*27 If you have some kind of access control on your website, you will have28 to send a 403 code for disallowed resources. So make sure the user29 is not lost when they try to access a forbidden resource.3031*410 Gone*32 Did you know that there the "404 Not Found" has a brother named "41033 Gone"? Few people actually implement that, but the idea is that34 resources that previously existed and got deleted answer with 41035 instead of 404. If you are not deleting documents permanently from36 the database but just mark them as deleted, do the user a favour and37 use the 410 code instead and display a message that what they were38 looking for was deleted for all eternity.3940*500 Internal Server Error*41 Usually happens on programming errors or if the server is overloaded.Merge pull request #1191 from nluchs/patch-2Markus Unterwaditzerbefore adoptiona18dedc
line 42why, one step back42 A terribly good idea is to have a nice page there, because yourMerge pull request #863 from jet-sun/patch-1Daniel Neuhäuserbefore adoption5d20501
lines 43–79why, one step back43 application *will* fail sooner or later (see also:44 :ref:`application-errors`).454647Error Handlers48--------------4950An error handler is a function, just like a view function, but it is51called when an error happens and is passed that error. The error is most52likely a :exc:`~werkzeug.exceptions.HTTPException`, but in one case it53can be a different error: a handler for internal server errors will be54passed other exception instances as well if they are uncaught.5556An error handler is registered with the :meth:`~flask.Flask.errorhandler`57decorator and the error code of the exception. Keep in mind that Flask58will *not* set the error code for you, so make sure to also provide the59HTTP status code when returning a response.6061Here an example implementation for a "404 Page Not Found" exception::6263 from flask import render_template6465 @app.errorhandler(404)66 def page_not_found(e):67 return render_template('404.html'), 4046869An example template might be this:7071.. sourcecode:: html+jinja7273 {% extends "layout.html" %}74 {% block title %}Page Not Found{% endblock %}75 {% block body %}76 <h1>Page Not Found</h1>77 <p>What you were looking for is just not there.78 <p><a href="{{ url_for('index') }}">go somewhere nice</a>79 {% endblock %}