errorpages.rst
This file
Path
docs/patterns/errorpages.rst
77 lines · 2.9 KiB
Last changed by
Not known.
History of this fileTake it
The walk stopped after 200 commits: lines older than dc05722 point at it.
Made the foreword less defensive.Armin Ronacherbefore adoptiondc05722
lines 1–77why, one step back1Custom Error Pages2==================34Flask comes with a handy :func:`~flask.abort` function that aborts a5request with an HTTP error code early. It will also provide a plain black6and white error page for you with a basic description, but nothing fancy.78Depending on the error code it is less or more likely for the user to9actually see such an error.1011Common Error Codes12------------------1314The following error codes are some that are often displayed to the user,15even if the application behaves correctly:1617*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.2324*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 they try to access a forbidden resource.2829*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 they were36 looking for was deleted for all eternity.3738*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`).434445Error Handlers46--------------4748An error handler is a function, just like a view function, but it is49called when an error happens and is passed that error. The error is most50likely a :exc:`~werkzeug.exceptions.HTTPException`, but in one case it51can be a different error: a handler for internal server errors will be52passed other exception instances as well if they are uncaught.5354An error handler is registered with the :meth:`~flask.Flask.errorhandler`55decorator and the error code of the exception. Keep in mind that Flask56will *not* set the error code for you, so make sure to also provide the57HTTP status code when returning a response.5859Here an example implementation for a "404 Page Not Found" exception::6061 from flask import render_template6263 @app.errorhandler(404)64 def page_not_found(e):65 return render_template('404.html'), 4046667An example template might be this:6869.. sourcecode:: html+jinja7071 {% 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 %}