Python 100 project #17: 404 page for Flask

So far the website I created, there is no error handling. First of all, I implemented 404 page, which is used in case there is no page(or files) found for the requested directory.

 

Output Example:

– Default 404 page … This page does serve the what it’s supposed to (to inform the user there is no file found), but I like it to be more unique.

– Customized 404 page … I pasted a image so that it looks more distinctive. At this moment, it’s not responsive at all, and the image size is fixed.

 

Here is the changes I made (for full history you can check my github repository):

– This is 404 page html template.

{% extends "base.html" %}

{% block content %}
    <h1>File Not Found</h1>
    <img src="https://s3.amazonaws.com/your-bucket/your-404-image.jpg" alt="404 image"/>
    <p><a href="{{ url_for('index') }}">Back</a></p>
{% endblock %}

– This is error handling module. I used separate module rather than using existing routes module.

from flask import render_template

from app import app

@app.errorhandler(404)
def not_found_error(error):
    return render_template('404.html'), 404

– This is init file to include newly created error routing.

from flask import Flask

from config import Config

app = Flask(__name__)
app.config.from_object(Config)

from app import routes, errors