Question

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?

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top