Skip to content
Myr

errorpages.rst

This file

Path

docs/patterns/errorpages.rst

83 lines · 3.1 KiB

Last changed by

Merge pull request #1413 from skybert/500-errhandler-noteKeyan Pishdadianbefore adoption76c518d

History of this file

Take it

The walk stopped after 200 commits: lines older than 41b5d77 point at it.

Merge pull request #1273 from prakashpp/patch-1Markus Unterwaditzerbefore adoption41b5d77

lines 1–60why, one step back
1.. _errorpages:
23Custom 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.42 A terribly good idea is to have a nice page there, because your43 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.60

Merge pull request #1413 from skybert/500-errhandler-noteKeyan Pishdadianbefore adoption76c518d

lines 61–63why, one step back
61Please note that if you add an error handler for "500 Internal Server
62Error", Flask will not trigger it if it's running in Debug mode.63

Merge pull request #1273 from prakashpp/patch-1Markus Unterwaditzerbefore adoption41b5d77

lines 64–82why, one step back
64Here an example implementation for a "404 Page Not Found" exception::
6566 from flask import render_template6768 @app.errorhandler(404)69 def page_not_found(e):70 return render_template('404.html'), 4047172An example template might be this:7374.. sourcecode:: html+jinja7576 {% extends "layout.html" %}77 {% block title %}Page Not Found{% endblock %}78 {% block body %}79 <h1>Page Not Found</h1>80 <p>What you were looking for is just not there.81 <p><a href="{{ url_for('index') }}">go somewhere nice</a>82 {% endblock %}

Merge pull request #1413 from skybert/500-errhandler-noteKeyan Pishdadianbefore adoption76c518d

line 83why, one step back
83