SQL Log Middleware

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
from django.db import connection
from django.template import Template, Context

class SQLLogMiddleware:

    def process_response ( self, request, response ): 
   
        t = Template('''
            <ul class="sqllog">
                {% for sql in sqllog %}
                    <li>{{ sql.sql }}</li>
                {% endfor %}
            </ul>
        ''')

        response.content = "%s%s" % ( response.content, t.render(Context({'sqllog':connection.queries})))

More like this

  1. SQL Log Middleware - with multiple databases by guglielmocelata 3 years, 2 months ago
  2. SQL Log Middleware w/query count & exec time by tobias 7 years ago
  3. SQL Log To Console Middleware by davepeck 4 years, 8 months ago
  4. Color SQL logging middleware by moe 6 years, 2 months ago
  5. SQL Log Middleware + duplicates by guettli 6 years, 9 months ago

Comments

rlawson (on March 30, 2007):

this worked great but I had to add the following line to return a response

...

response.content = "%s%s" % ( response.content, t.render(Context({'sqllog':connection.queries})))

return response

#

guettli (on July 27, 2007):

I improved Snippet 161 in Snippet 344.

It counts duplicated SQL queries.

#

(Forgotten your password?)