Learn Django

Creating Public Error Pages

In the previous lesson you configured logging and reporting so that you, as the developer, are notified when errors occur so you can go fix the problem. However, this doesn't really help the end user as they're only shown a simple error page. As it is, if the user does encounter an error they are sent to a white page with an unhelpful message. It would be more helpful to let them know that an error was encountered and perhaps give them a link back to a working page.

Create the 500 Error Page

The HTTP error code for an internal server error is 500. This is a generic catch all error that is used when a more specific error isn't raised. You can see the complete list of 500 error messages for more information.

You can set up Django to display a template if a HTTP 500 error is encountered. This should be a very basic page because, if this is an internal server error, you can't depend on all of Django working properly. Follow these steps to do so:

  1. Create a file named 500.html in /.../crmeasy/crmapp/templates
  2. Open that file in your IDE and enter in this code:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
<!DOCTYPE html>

<html lang="en">

    <head>
        <title>Sorry, there's been an error | CRM Easy</title>
    </head>

    <body>

        <h1>Sorry, there's been an error</h1>
        <p>Our engineers have been notified.</p>
        <p>Please try to access the <a href="/">home page</a>.</p>

    </body>

</html>

Create the 404 Error Page

Sometimes users try to access URLs on your site that don't exist. When that happens a HTTP 404 error is generated. Django can respond with a certain template file when these errors are encountered. Follow these steps to set it up.

  1. Create a file named 404.html in /.../crmeasy/crmapp/templates
  2. Open it into your IDE and enter this code:
1
2
3
4
5
6
7
8
9
{% extends "base.html" %}

{% block title %}That page doesn't exist | CRM Easy{% endblock %}

{% block content %}
<h1>That page doesn't exist</h1>

<p>You can easily start back at the <a href="/">home page</a>!</p>
{% endblock %}

Push changes to staging and production

Now you can update the staging and production Heroku apps to have the new logging configuration.

Go through these steps for each environment:

1
2
3
4
(venv)$ git add .
(venv)$ git commit -m "created 500 and 404 error pages"
(venv)$ git push staging master
(venv)$ git push prod master
Track your progress with a free account