Thursday, September 17, 2015

Error handling

The worst feeling in the world is seeing that ugly default page whenever you stumble across an error in your Django app. Make lemonade out of those lemons by customizing your error pages like so:

Add this to urls.py:
handler404 = 'eat_decisive.views.handler404'
handler500 = 'eat_decisive.views.handler500'

Add this to views.py:
def handler404(request):
    return render(request, '404.html')

def handler500(request):
    return render(request, '500.html')

---

Create your html files (place in top level of your templates folder)
404.html
       

{% extends 'eat_decisive/base.html' %}

{% load staticfiles %}

{% block title %}Page not found {% endblock title %}

{% block body_block %}
<h1> YOUR ERROR HEADLINE HERE. </h1>

<p> YOUR ERROR MESSAGE HERE. </p>
{% endblock body_block %}


Now create a similar file for 500.html.

I added a Jane Austen quote to my 404 page, just for kicks. And because I love the phrase "intolerably stupid." Gotta remember to use that more often.


No comments:

Post a Comment