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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With