Template Query Debug

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
{% if debug %}
<div id="debug">
  <h2>Queries</h2>
  <p>
    {{ sql_queries|length }} Quer{{ sql_queries|pluralize:"y,ies" }}
    {% ifnotequal sql_queries|length 0 %}
    (<span style="cursor: pointer;" onclick="var s=document.getElementById('debugQueryTable').style;s.display=s.display=='none'?'':'none';this.innerHTML=this.innerHTML=='Show'?'Hide':'Show';">Show</span>)
    {% endifnotequal %}
  </p>
  <table id="debugQueryTable" style="display: none;">
    <col width="1"></col>
    <col></col>
    <col width="1"></col>
    <thead>
    <tr>
      <th scope="col">#</th>
      <th scope="col">SQL</th>
      <th scope="col">Time</th>
    </tr>
    </thead>
    <tbody>
    {% for query in sql_queries %}<tr class="{% cycle odd,even %}">
      <td>{{ forloop.counter }}</td>
      <td>{{ query.sql|escape }}</td>
      <td>{{ query.time }}</td>
    </tr>{% endfor %}
    </tbody>
  </table>
</div>
{% endif %}

More like this

  1. Debug SQL Query in Template by dario.agliottone 5 days, 23 hours ago
  2. Output sql_queries in Firebug console when in debug mode by wojas 2 years, 2 months ago
  3. testdata tag for templates by showell 3 years ago
  4. Paginator TemplateTag by trbs 4 years, 1 month ago
  5. outliner template tag by showell 3 years ago

Comments

jdobbins (on March 11, 2007):

Thanks for the great snip! Wonderful tool for debugging 'slow pages'!

#

msgre (on March 14, 2007):

Cool!

I have little troubles with displaying long rows of queries. For example:

SELECT col1,col2,col3,col4 FROM table;

If you have a LOT of colX, they aren't wrapped in browser, and queries are bad to read.

So, I make little modification. I wrote custom filter, which replace character "," (comma) to ", " (comma and space). Now I could read debug SQL queries more comfortably.

My custom filter

def spaces_and_commas(value):
    from re import compile
    c = compile(",[^ ]")
    return c.sub(", ", value)

register.filter(spaces_and_commas)

Modification of your template (row 24)

<td>{{ query.sql|spaces_and_commas|escape }}</td>

Thank you for great snippet, insin.

#

cookiebearo (on May 28, 2007):

thank you both insin for the snippet and msgre for the filter, however, i noticed a bug with the filter

the expression:

c = compile(",[^ ]")

matches the first character after the comma (non-space), so after the sub call it is replacing that character with a space

i only caught this after realizing there was a missing " before each column

i swapped that out for:

c = compile(",(?! )")

and it seems to be working a-ok

#

PhiR (on September 12, 2007):

I'm using this snippets with the modifications in the comments but I've found useful to add this templatetags:

def slice_500(value):
    return value[:500]
register.filter(slice_500)

and to restrict the number of queries printed like this:

{% for query in sql_queries|splice_500 %}<tr class="{% cycle odd,even %}">

Some of my views are used for validation and run up to a few hundred thousand queries, which is a bit too much to print in a browser.

#

darek (on July 13, 2008):

Very helpful snippen. Tanks.

By the way... in current Django SVN version You don't have to set TEMPLATE_CONTEXT_PROCESSORS, it's on by default.

#

aaronfay (on September 24, 2008):

Brilliant works like a charm :)

af

#

ramikassab (on February 11, 2009):

Awesome snippet. Simple, clean, and effective...

#

analyzer (on March 7, 2010):

Hello,

I am new to django and i justed started to play around with django. I've copied this snippet to my base template but I can't see anything?

I checked every of the three steps mentioned above, the context processors are set by default and my ip is on INTERNAL_IPS. I don't understand what the third point means but I've added a request context to shortcut for rendering templates.

What can I do to get it work? Thanks for your help in advance and sorry for my bad english.

analyzer

#

Klaidi (on July 22, 2010):

nice !

#

RoslynRowe23 (on May 18, 2011):

Do you acknowledge that it's high time to receive the personal loans, which will realize your dreams.

#

chunhairr (on March 7, 2012):

Hello, everybody, the good shoping place, the new season approaching, click in. Welcome to ==== http://www.proxy4biz.com == Air Jordan (1-24) shoes $35 Jordan (1-22)&2009 shoes $45 Nike shox (R4, NZ, OZ, TL1, TL2, TL3) $35 Handbags ( Coach Lv fendi D&G) $30 T-shirts (polo, ed hardy, lacoste) $14 Jean (True Religion, ed hardy, coogi)$34 Sunglasses ( Oakey, coach, Gucci, Armaini)$15 New era cap $16 Biki ni (Ed hardy, polo) $18
FREE SHIPPING http://www.proxy4biz.com

http://www.proxy4biz.com

http://www.proxy4biz.com

http://www.proxy4biz.com http://www.proxy4biz.com

http://www.proxy4biz.com

http://www.proxy4biz.com

http://www.proxy4biz.com

===( http://www.proxy4biz.com )===

===( http://www.proxy4biz.com )===

===( http://www.proxy4biz.com )===

===( http://www.proxy4biz.com )===

===( http://www.proxy4biz.com )===

===( http://www.proxy4biz.com )=== This is a shopping paradise We need your support and trust 、

#

2egoo (on April 14, 2012):

2egoo . com offers products and services of great value for the shoppers and try to make a large clientele. Please feel free to have a look around and make contact with us if you have any questions. We have become a company representative and distributor of compatible electronics accessories, especially of laptop accessories: [url=http://www.2egoo.com/]All Categories[/url], [url=http://www.2egoo.com/cpucoolingfan/cpucoolingfan.html]CPU Fan[/url], [url=http://www.2egoo.com/inverter/inverter.html]Inverter[/url], [url=http://www.2egoo.com/powerjack/powerjack.html]Power Jack[/url], [url=http://www.2egoo.com/keyboard/keyboard.html]Keyboard[/url], [url=http://www.2egoo.com/hinge/hinge.html]Hinge[/url], [url=http://www.2egoo.com/videocable/videocable.html]Video Cable[/url], [url=http://www.2egoo.com/batterycharger/batterycharger.html]Charger[/url]; and even dslr camera and cam corder batteries and chargers, diy equipment batteries, and also other products.For more information: *www.2egoo.com* **Apple MacBook 13 inch MA472SA A CPU Cooling Fan$26.99 ** === ===Apple MacBook 13 inch MA701 A CPU Cooling Fan Shop Price: $26.99=== ====== ===

#

2egoo (on April 14, 2012):

In order to acquire the best brand-new Digital Camera battery at most favored price, you have got visited the best place. This site offers the highest quality battery while using the lowest price considering the highest higher level of service, all in a very secure and convenient platform. Our range of products include replacement Camera battery for almost all famous labels, including Acer Battery , BenQ Battery, Canon Battery, Casio Battery, Fujifilm Battery, HP Battery, Kodak Battery, Nikon Battery, Olympus Battery, Panasonic Battery, Ricoh Battery, Samsung Battery, and many others. To read more: www.aa-battery-charger.co.uk/!!

#

(Forgotten your password?)