Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Adding handler500 to urls.py and 400/500 error templates

In trying to make snowy a bit prettier, google found this:
http://www.arthurkoziel.com/2009/01/15/passing-mediaurl-djangos-500-error-view/
  • Loading branch information...
commit 9f1bbd549bde3fb6285c7ad34c29587319a52f80 1 parent eb978cb
Jeff Schroeder SEJeff authored
3  site_media/css/screen.css
@@ -383,3 +383,6 @@ table.input-form {
383 383 text-align: center;
384 384 }
385 385
  386 +.center {
  387 + text-align: center;
  388 +}
6 templates/404.html
... ... @@ -0,0 +1,6 @@
  1 +{% extends "base.html" %}
  2 +{% block sidebar-container %}{% endblock %}
  3 +{% block content %}
  4 +<p class="center"> <img src="{{ MEDIA_URL }}img/face-sad.png"> </p>
  5 +<p class="center"><h1 class="center">We're sorry, but {{ request_path }} is a broken link.</h1>
  6 +{% endblock %}
6 templates/500.html
... ... @@ -0,0 +1,6 @@
  1 +{% extends "base.html" %}
  2 +{% block sidebar-container %}{% endblock %}
  3 +{% block content %}
  4 +<p class="center"> <img src="{{ MEDIA_URL }}img/face-sad.png"> </p>
  5 +<p class="center"><h1 class="center">We're sorry, but something went wrong and caused a 500 server error.</h1>
  6 +{% endblock %}
2  urls.py
@@ -22,6 +22,8 @@
22 22 from django.contrib import admin
23 23 admin.autodiscover()
24 24
  25 +handler500 = 'snowy.views.server_error'
  26 +
25 27 urlpatterns = patterns('',
26 28 url(r'^$', direct_to_template, {'template': 'index.html'},
27 29 name='snowy_index'),
17 views.py
... ... @@ -1,5 +1,22 @@
  1 +from django import http
  2 +from django.conf import settings
  3 +from django.template import Context, loader
1 4 from django.http import HttpResponseRedirect
2 5 from django.core.urlresolvers import reverse
3 6
4 7 def user_index(request, username):
5 8 return HttpResponseRedirect(reverse("note_index", args=([username])))
  9 +
  10 +def server_error(request, template_name='500.html'):
  11 + """
  12 + 500 error handler.
  13 +
  14 + Templates: `500.html`
  15 + Context:
  16 + MEDIA_URL
  17 + Path of static media (e.g. "media.example.org")
  18 + """
  19 + t = loader.get_template(template_name) # You need to create a 500.html template.
  20 + return http.HttpResponseServerError(t.render(Context({
  21 + 'MEDIA_URL': settings.MEDIA_URL
  22 + })))

0 comments on commit 9f1bbd5

Please sign in to comment.
Something went wrong with that request. Please try again.