究竟在一次Request中,Django對(duì)數(shù)據(jù)庫(kù)執(zhí)行了那些查詢和操作呢?呵呵,Django早就為我們想好了這個(gè)問(wèn)題,使用django.core.context_processors.debug模塊即可。
在setting中設(shè)置:
TEMPLATE_CONTEXT_PROCESSORS = (
"django.core.context_processors.debug", #debug 一次請(qǐng)求調(diào)用到多少SQL語(yǔ)句",
)
并設(shè)置能看到次debug信息的請(qǐng)求IP:
INTERNAL_IPS = ('127.0.0.1',)
我們就可以在模板中設(shè)置一下,即可:
{% endblock %}
{% if sql_queries %}
<h3>SQL excute in this Request</h3>
<!-- debug: show the sql excute in this request -->
{% for query in sql_queries %}<h3>Excute times: {{query.time}}</h3>
<p>
<code>
{{query.sql}}
</code>
</p>
{% endfor %}<!-- debug ends here -->
{% endif %}
以上只會(huì)在你設(shè)置了TEMPLATE_DEBUG = DEBUG,和請(qǐng)求IP在INTERNAL_IPS設(shè)置過(guò),才會(huì)顯示。