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
- SQL Log Middleware w/query count & exec time by tobias 5 years, 1 month ago
- SQL Log Middleware - with multiple databases by guglielmocelata 1 year, 3 months ago
- TerminalLoggingMiddleware by heckj 4 years, 11 months ago
- Color SQL logging middleware by moe 4 years, 3 months ago
- SQL Log To Console Middleware by davepeck 2 years, 9 months ago
Comments
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
#
I posted an updated version of this with the above fix, a SQL query count, and execution time to:
www.djangosnippets.org/snippets/161/
#
I improved Snippet 161 in Snippet 344.
It counts duplicated SQL queries.
#