6

Jan

Filed in Code, Django, How-To's |

Today (for many hours) I sat here attempting to debug an error which Django wasn’t spitting out. This happened when combining Django+mod_wsgi and having a runtime import error. The result is a generic white internal server error page from Apache, and nothing recorded in any kind of error log.

So, to make your life easier, I thought I’d help you out by showing what I discovered.

First things first, you’re going to want to install a piece of debugging wsgi middleware. This was new to me, but its basically just a decorating class for your wsgi application. What I used was Paste’s ErrorMiddleware:

pip install -e svn+http://svn.pythonpaste.org/Paste/trunk

Now, pop open your handler file (handler.wsgi in our case). What we did, was make it so if DEBUG is set to True, then it wraps the application handler with the new middleware:

import os, sys, os.path
 
# Add the project to the python path
sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..'))
# Prevent mod_wsgi from erroring on stdout access
sys.stdout = sys.stderr
 
# Set our settings module
os.environ['DJANGO_SETTINGS_MODULE']='iplatform.settings'
 
import django.core.handlers.wsgi
 
# Run WSGI handler for the application
application = django.core.handlers.wsgi.WSGIHandler()
 
from django.conf import settings
 
# Debug middleware
if settings.DEBUG:
    print >> sys.stderr, "Using Paste error middleware"
    from paste.exceptions.errormiddleware import ErrorMiddleware
    application = ErrorMiddleware(application, debug=True, show_exceptions_in_wsgi_errors=True)
 
# Create session directory if not present 
if settings.SESSION_FILE_PATH:
    try:
        os.makedirs(settings.SESSION_FILE_PATH)
    except OSError:
        pass

Hope this helps you as much as it helped me!

  • Pascal

    I didn’t try the paste middleware so far, but what is to be said against the Werkzeug interactive debugger or Flickzeug?

  • Rin Tinner

    There’s something a bit futzy if nothing is being recorded in any of your error logs.

  • David

    @Pascal, I’m definitely going to be looking into Werkzeug’s tools for future uses now that I realize how simply WSGI middleware can be :)

  • http://komunitasweb.com/2010/02/django-101/ Django 101 | KomunitasWeb

    [...] Debugging Django Errors (January 2010) [...]

  • http://www.air-jordan-10.com/ air jordan 10

    Well , the view of the passage is totally correct ,your details is really reasonable and you guy give us valuable informative post, I totally agree the standpoint of upstairs. I often surfing on this forum when I m free and I find there are so much good information we can learn in this forum! http://spoon8.net/

  • http://www.pariuri-x.ro/ Pariuri sportive

    i didn't try it , but it's interesting

  • http://www.moncler-down-jackets.com moncler jackets

    Thank you very much for sharing!!To top!louis vuitton outlet is a professional webside which offer lots of common sense of life and I have learnt a lot since my friends recommended it to me.There are several articles about this theme in Louis vuitton bags,come on and have a discussion! air jordan 7

  • http://yohan06.student.ipb.ac.id Yohan

    i just know django, thank you :)

  • http://www.henweekends.net/barcelona-weekend things to do on a hen night

    You're seeing this error because you have DEBUG = True in your Django settings file. Change that to False, and Django will display a standard 404 page.”

  • http://www.allvoices.com/contributed-news/5807173-tej-kohli-ipad-review Tej Kohli

    Well Some type Automatic debugger is required

blog comments powered by Disqus