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



commit 9f1bbd549bde3fb6285c7ad34c29587319a52f80
Author: Jeff Schroeder <jeffschroeder computer org>
Date:   Mon Nov 1 18:54:01 2010 -0700

    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/

 site_media/css/screen.css |    3 +++
 templates/404.html        |    6 ++++++
 templates/500.html        |    6 ++++++
 urls.py                   |    2 ++
 views.py                  |   17 +++++++++++++++++
 5 files changed, 34 insertions(+), 0 deletions(-)
---
diff --git a/site_media/css/screen.css b/site_media/css/screen.css
index a7d8d4c..2f582dd 100644
--- a/site_media/css/screen.css
+++ b/site_media/css/screen.css
@@ -383,3 +383,6 @@ table.input-form {
     text-align: center;
 }
 
+.center {
+    text-align: center;
+}
diff --git a/templates/404.html b/templates/404.html
new file mode 100644
index 0000000..bd9a625
--- /dev/null
+++ b/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 %}
diff --git a/templates/500.html b/templates/500.html
new file mode 100644
index 0000000..bd42eca
--- /dev/null
+++ b/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 %}
diff --git a/urls.py b/urls.py
index cfcf6e9..ff767a8 100644
--- a/urls.py
+++ b/urls.py
@@ -22,6 +22,8 @@ from django.conf.urls.defaults import *
 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'),
diff --git a/views.py b/views.py
index a1cdd86..cf1c6fd 100644
--- a/views.py
+++ b/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
+    })))



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]