Django: how can I apply a middleware action only to certain filetypes?
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_response
rather than process_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.
精彩评论