Menu

Flask: Stop Requests the Standard Way with abort() — 404/403 with Custom Error Pages in 5 Minutes
📰

Flask: Stop Requests the Standard Way with abort() — 404/403 with Custom Error Pages in 5 Minutes

DEV Community: flask·Linh Fish Dev·3 days ago
#81NFhtsa
#devtoflask#devto#dev#flask#error
Reading 0:00
15s threshold

When a user opens a post that does not exist, or visits the admin area without permission, the view must stop immediately and return the correct error code — not render an error template by hand and return it with status 200. Flask ships with abort() : it raises an HTTP exception, stops the view right there, and Flask returns the right status code with an error page. Combined with errorhandler() , you get a clean, standard error page for the whole application.

The Problem

The most common approach is to return an error string with a status code by hand: return "Post not found", 404 . It works, but every view repeats the same pattern, and it is easy to forget the status code — leave out , 404 and the error page returns 200, so Google indexes your “not found” page and clients or APIs cannot tell the request failed.…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.