views:

35

answers:

1

i've write a middware like this:

class LogMiddleware( object ):

def process_request( self, request ):
    logging.debug("start")

def process_response( self, request, response ):
    logging.debug("end")
    return response

and i put it in the bottom of MIDDLEWARE_CLASSES

most time it works fine.

and when i test with url /admin without an trailing "/" and i could only see the "end" logged, why?

+4  A: 

The documentation explains this.

Middleware classes are processed in the order they appear. The CommonMiddleware class is higher up than your LogMiddleware class, so is processed first. It performs a redirect because your URL doesn't end with a slash, so returns an HttpResponseRedirect.

If a request middleware returns a response, as in this case, no further request middleware classes are processed, so 'start' is not logged. However, response middleware classes are always processed, so 'end' is logged.

Daniel Roseman