U
U
un1t2015-08-13 11:00:58
Django
un1t, 2015-08-13 11:00:58

Djnago - devserver makes queries to the database when returning static and media images, how to disable?

This is what each image request looks like

DEBUG 2015-08-13 10:55:47,774 14740 utils (0.000) SET SQL_AUTO_IS_NULL = 0; args=()
DEBUG 2015-08-13 10:55:47,780 14740 utils (0.000) SET SQL_AUTO_IS_NULL = 0; args=()
DEBUG 2015-08-13 10:55:47,786 14740 utils (0.000) SELECT `django_session`.`session_key`, `django_session`.`session_data`, `django_session`.`expire_date` FROM `django_session` WHERE (`django_session`.`session_key` = '6wi8pc3ted8yjjc3s6h4zz18s3x6irl5' AND `django_session`.`expire_date` > '2015-08-13 07:55:47'); args=('6wi8pc3ted8yjjc3s6h4zz18s3x6irl5', '2015-08-13 07:55:47')
[13/Aug/2015 10:55:47]"GET /static/images/eye-icon.png HTTP/1.1" 304 0

It is clear that etol does
'django.contrib.sessions.middleware.SessionMiddleware',
but how to disable this thing just for static?
Well, of course you can inherit from this thing, but maybe there is some generally accepted way. I'm not the only one with this problem, am I?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Stanislav Fateev, 2015-08-13
@svfat

It seems to me that this is not due to a request for a picture, but because of a session request. What if sessions are disabled in MIDDLEWARE_CLASSES and INSTALLED_APPS?
Well, or handing out statics is not dzhangoy.
UPD : Alternatively, override process_request and process_response for django.contrib.sessions.middleware.SessionMiddleware. The code has not been tested.

from django.contrib.sessions.middleware import SessionMiddleware

class MySessionMiddleware(SessionMiddleware):
    def process_request(self, request):
        if request.path_info.startswith('/static/'):
            return None
        super(MySessionMiddleware, self).process_request(request)

    def process_response(self, request, response):
        if request.path_info.startswith('/static/'):
            return response
        return super(MySessionMiddleware, self).process_response(request, response)

S
sim3x, 2015-08-13
@sim3x

This happens (even in production) with every http request
In order to get rid of this everywhere, you need to transfer pictures (statics) to a subdomain or another domain

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question