Login

SQL Log Middleware

Author:
joshua
Posted:
February 28, 2007
Language:
Python
Version:
Pre .96
Score:
4 (after 4 ratings)

This middleware will add a log of the SQL queries executed at the bottom of every page. You can (should) use BeautifulSoup to place this in a specific location.

Note: If you serve non-html content, it would be wise to do a mimetype check.

 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. Template tag - list punctuation for a list of items by shapiromatron 11 months, 2 weeks ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 11 months, 3 weeks ago
  3. Serializer factory with Django Rest Framework by julio 1 year, 6 months ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 7 months ago
  5. Help text hyperlinks by sa2812 1 year, 7 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.

#

Please login first before commenting.