Movatterモバイル変換


[0]ホーム

URL:


Open In App
Next Article:
Deploy Python Flask App on Heroku
Next article icon

A 404 Error occurs when a page is not found. This can happen due to several reasons:

To improve user experience, websites should have a custom error page instead of showing a generic, unappealing one. GeeksforGeeks also uses a custom error page. For example, if we visitwww.geeksforgeeks.org/ajneawnewiaiowjf, we'll see it in action.

Default 404 Error

GeeksForGeeks Customized Error Page

GFG-custom-error404-page
GFG Custom Error page

A custom error page improves user experience by providing a clean layout, navigation options, or auto-redirecting to the homepage. Flask lets us handle errors and display a custom page easily.

Before diving further in to the topic, make sure toInstall and set up Flask

We will createapp.py to manage templates,404.html to handle 404 errors, andheader.html for the website's header and navbar. Let's look at their code one by one:

app.py

Flask allows defining routes and functions in aPython file so create anapp.pyfile for our main flask app. We set up the main page route ('/') and a 404 error handler using Flask's built-in function.

PYTHON
fromflaskimportFlask,render_templateapp=Flask(__name__)# app name@app.errorhandler(404)# inbuilt function which takes error as parameterdefnot_found(e):# defining functionreturnrender_template("404.html")

The above python program will return404.html file whenever the user opens a broken link.

header.html

The header.html file is areusable template that contains the navbar and common page structure. It helps keep the code clean by avoiding repetition and makes updates easier. Other pages, like 404.html, extend it to maintain a consistent layout.

HTML
{% extends "header.html" %}<!-- Exports header and navbar from header.html     or any file you want-->{% block title %}Page Not Found{% endblock %}{% block body %}<h1>Oops! Looks like the page doesn't exist anymore</h1><ahref="{{ url_for('index') }}"><p>Click Here</a>To go to the Home Page</p><!-- {{ url_for('index') }} is a var which returns url of index.html-->{% endblock %}

Automatically Redirecting to the Home page

Theapp.py code for this example stays the same as above. The following code Shows the Custom 404 Error page and starts a countdown of5 seconds. After 5 seconds are completed, it redirects the user back to the homepage.

404.html

Following code exports header and navbar fromheader.html. Both files should be stored in the templates. After 5 seconds, the user will getredirected to the Home Page Automatically.

HTML
{% extends "header.html" %}{% block title %}Page Not Found{% endblock %}{% block body %}<h1>Oops! Looks like the page doesn't exist anymore</h1><pid="pageInfo">Redirecting to Home Page after 5 seconds...</p><script>varseconds=5;varurl="{{ url_for('index') }}";functionredirect(){if(seconds<=0){window.location=url;}else{seconds--;document.getElementById("pageInfo").innerHTML="Redirecting to Home Page after "+seconds+" seconds.";setTimeout(redirect,1000);}}window.onload=redirect;</script>{% endblock %}

Output

Let's run the app using command - "python app.py", it will rener the home page which will look like this -

homepage-ofcustom-error-page
Home page

Now let's try to visit some random URL "http://127.0.0.1:5000/random_url", the custom error page will look like this -

random-url-custom-error
Custom error page

Improve
Practice Tags :

Similar Reads

We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood ourCookie Policy &Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences

[8]ページ先頭

©2009-2025 Movatter.jp