Skip to content
Browse files

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...
1 parent eb978cb commit 9f1bbd549bde3fb6285c7ad34c29587319a52f80 @SEJeff SEJeff committed Nov 1, 2010
Showing with 34 additions and 0 deletions.
  1. +3 −0 site_media/css/screen.css
  2. +6 −0 templates/404.html
  3. +6 −0 templates/500.html
  4. +2 −0 urls.py
  5. +17 −0 views.py
View
3 site_media/css/screen.css
@@ -383,3 +383,6 @@ table.input-form {
text-align: center;
}
+.center {
+ text-align: center;
+}
View
6 templates/404.html
@@ -0,0 +1,6 @@
+{% extends "base.html" %}
+{% block sidebar-container %}{% endblock %}
+{% block content %}
+<p class="center"> <img src="{{ MEDIA_URL }}img/face-sad.png"> </p>
+<p class="center"><h1 class="center">We're sorry, but {{ request_path }} is a broken link.</h1>
+{% endblock %}
View
6 templates/500.html
@@ -0,0 +1,6 @@
+{% extends "base.html" %}
+{% block sidebar-container %}{% endblock %}
+{% block content %}
+<p class="center"> <img src="{{ MEDIA_URL }}img/face-sad.png"> </p>
+<p class="center"><h1 class="center">We're sorry, but something went wrong and caused a 500 server error.</h1>
+{% endblock %}
View
2 urls.py
@@ -22,6 +22,8 @@
from django.contrib import admin
admin.autodiscover()
+handler500 = 'snowy.views.server_error'
+
urlpatterns = patterns('',
url(r'^$', direct_to_template, {'template': 'index.html'},
name='snowy_index'),
View
17 views.py
@@ -1,5 +1,22 @@
+from django import http
+from django.conf import settings
+from django.template import Context, loader
from django.http import HttpResponseRedirect
from django.core.urlresolvers import reverse
def user_index(request, username):
return HttpResponseRedirect(reverse("note_index", args=([username])))
+
+def server_error(request, template_name='500.html'):
+ """
+ 500 error handler.
+
+ Templates: `500.html`
+ Context:
+ MEDIA_URL
+ Path of static media (e.g. "media.example.org")
+ """
+ t = loader.get_template(template_name) # You need to create a 500.html template.
+ return http.HttpResponseServerError(t.render(Context({
+ 'MEDIA_URL': settings.MEDIA_URL
+ })))

0 comments on commit 9f1bbd5

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