views:

119

answers:

3

I have developed an application using Django and everything is working fine but I don't know what's going on behind the scenes. I would like to know:

  • How many times the database is being hit for each request?
  • What was the execution time for each query?
  • How long it took to render the template?
  • Regular profiling info (ncalls, tottime per function).

Is there a Middleware to handle this that I could install? Which are the best practices to profile my views?

Thanks

A: 
{% 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 %}

Django Snippet: Template Query Debug

Antony Hatchkins
Very interesting, thanks! I will try it.
jbochi
+2  A: 

three words: Django Debug Toolbar

piquadrat
Looks fantastic. I will test it. Thank you!
jbochi
+2  A: 

The one project that meets all your requirements, with the exception of profiling, is the excellent django debug toolbar.

For standard profiling you need to use repoze.profile (which means you have to be running Django with WSGI interface such as mod_wsgi).

If you're hardcore and need to debug memory leaks use dozer (also a WSGI component).

Van Gale