I’m working in Django, and I want to apply some middleware ONLY to HTML pages – not to images, js, css etc. This is to reduce the number of requests on the server.
This is my middleware code:
class checkLdapUser:
def process_request(self, request):
if response.META['CONTENT_TYPE'] is 'text/html':
# check the LDAP user here
else:
return None
How can I check the CONTENT_TYPE?
Thanks!
UPDATE:
working code:
def process_response(self, request, response):
if response.get('Content-Type', '')=='text/html':
# do stuff
return response
You’re on the right track, but you need to put that code in
process_responserather thanprocess_request. The latter runs before the request is handled by your view code, so it doesn’t have access to the response and its content type.