Custom middleware only works when debugging is on

114 views
Skip to first unread message

jwpeddle

unread,
Nov 20, 2008, 11:54:00 AM11/20/08
to Django users
I've got a custom middleware class (nearly identical in function to
the flat page fallback middleware). When I have DEBUG = True, it works
as expected. When I have DEBUG = False, I get errors stemming from
variables set by the middleware not existing for the templates. After
investigating, the only progress I've made is confirming that the
class is called somehow (a print statement just inside the class will
print) but the process_response never is.

Again, this works when debugging is on. Perfectly. Not at all when
it's off. Not on apache and not on the dev server. I apologize for the
lack of errors, but I haven't narrowed it down enough yet to get any
relevant errors.

class PageFallbackMiddleware():
def process_response(self, request, response):
if response.status_code != 404:
return response # No need to check for a flatpage for
non-404 responses.
try:
return page(request, request.path)
# Return the original response if any errors happened. Because
this
# is a middleware, we can't assume the errors will be caught
elsewhere.
except Http404:
return response
except:
if settings.DEBUG:
raise
return response

jwpeddle

unread,
Nov 20, 2008, 12:41:10 PM11/20/08
to Django users
Ok, I've tried to dumb things down, and it turns out the built-in
flatpage fallback middleware ALSO doesn't have it's process_response
called. I can only assume a response is sent before the middleware is
reached, but I've got a pretty standard setup:

MIDDLEWARE_CLASSES = (
'django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'applecore.main.middleware.PageFallbackMiddleware',
'django.contrib.redirects.middleware.RedirectFallbackMiddleware',
)

jwpeddle

unread,
Nov 20, 2008, 12:53:44 PM11/20/08
to Django users
Eliminated any possible issues in my urls conf. Even removing
everything from it doesn't change the situation.

On Nov 20, 1:54 pm, jwpeddle <jwped...@gmail.com> wrote:

jwpeddle

unread,
Nov 20, 2008, 1:58:58 PM11/20/08
to Django users
Oh my god the stupidity.

The error was in my 500.html template. NOTHING to do with my
middleware.

Sigh.

On Nov 20, 1:54 pm, jwpeddle <jwped...@gmail.com> wrote:

shi shaozhong

unread,
Nov 24, 2008, 7:42:46 AM11/24/08
to django...@googlegroups.com
Hello, there.

I am very much interested in this middleware stuff.

I have now a Python data processing script, which normally runs 2 to 3
minutes. I was trying to use feedback.py to call this external script
as a subprocess, firing it off to run, monitoring its progress, and
return an innerHTML message when the subprocess script finishes its
work.

Can this middleware do such work?

Basically, I need a simple and straightforward solution for the time being.

Your advice and assistance will be deeply appreciated.

Regards.

David

2008/11/20 jwpeddle <jwpe...@gmail.com>:
Reply all
Reply to author
Forward
0 new messages