We Recommend

The Definitive Guide to Django The Definitive Guide to Django
In The Definitive Guide to Django: Web Development Done Right, Adrian Holovaty, one of Django’s creators, and Django lead developer Jacob Kaplan–Moss show you how they use this framework to create award–winning web sites.


Posted By

jonhenshaw on 04/07/08


Tagged

debug template query


Versions (?)


Template Query Debug


Published in: Django 


URL: http://www.djangosnippets.org/snippets/93/

I often find something like this lurking at the end of my base templates - it'll show you which queries were run while generating the current page, but they'll start out hidden so as not to be a pain.

Of course, before this works, you'll need to satisfy all the criteria for getting debug information in your template context:

1. Have 'django.core.context_processors.debug' in your TEMPLATE_CONTEXT_PROCESSORS setting (it was there in the default settings, last time I checked).
2. Have your current IP in your INTERNAL_IPS setting.
3. Use RequestContext when rendering the current template (if you're using a generic view, you're already using RequestContext).


  1. {% if debug %}
  2. Queries
  3.  
  4. {{ sql_queries|length }} Quer{{ sql_queries|pluralize:"y,ies" }} {% ifnotequal sql_queries|length 0 %} (Show) {% endifnotequal %}
  5. {% for query in sql_queries %}{% endfor %}
  6. # SQL Time
  7. {{ forloop.counter }} {{ query.sql|escape }} {{ query.time }}
  8. {% endif %}

Report this snippet 

You need to login to post a comment.