Flask Tutorial – Custom Error Page in Flask

In this Flask Tutorial we are going to talk about creating Custom Error Page in Flask,

so when you are going to develop a web application or website, when a user write 

invalid route, the user get a 404 error page , instead of 404 error page we want to show them our

Custom Error Page in Flask. Flask allows an application to define Custom Error Page that can be

based on our template that we have, you can define Flask Custom Error Page like regular routes.

 

 

If you are interested in Web Development with Django Framework, than you can read the

complete articles in this link,  Django Web Development Tutorials.

 

 

 

 

So first of all this is our app.py file.

 

 

You can see that we have defined two new routes, the first one is for page_not_found()

and the second one is for internal_server_error(). 

 

 

 

 

 

Now we need to create our html files in the templates folder, especially we are going to create templates for our page_not_found() and internal_server_error() view functions.

 

 

 

 

templates/base.html

 

 

 

 

 

templates/index.html

 

 

 

 

 

templates/contact.html

 

 

 

 

 

Now we need to create our new html files for Custom Error Pages.

 

 

templates/404.html

 

 

 

 

 

templates/500.html

 

 

 

 

 

Now run the the Project and go to a wrong route, for example we are going to open

http://localhost:5000/hello, we don’t have the hello route, now you can see that

we have received a nice and customized error page.

 

Flask Tutorial - Custom Error Page in Flask
Flask Tutorial – Custom Error Page in Flask

 

 

 

 

Also you can watch my complete 4 hours training on Flask Web Development

Subscribe and Get Free Video Courses & Articles in your Email

 

Share via
Copy link
Powered by Social Snap
×