开发者

Django: how can I apply a middleware action only to certain filetypes?

开发者 https://www.devze.com 2023-02-22 12:56 出处:网络
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.

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消