Задать вопрос

Тел: +7 965 3737 888

561

Просмотров

12

Ответов

Template Query Debug

<p>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.</p>
<p>Of course, before this works, you'll need to satisfy all the criteria for getting debug information in your template context:</p>
<ol><li>Have 'django.core.context_processors.debug' in your TEMPLATE_CONTEXT_PROCESSORS setting (it was there in the default settings, last time I checked).</li>
<li>Have your current IP in your INTERNAL_IPS setting.</li>
<li>Use <a href="http://www.djangoproject.com/documentation/templates_python/#subclassing-context-requestcontext">RequestContext</a> when rendering the current template (if you're using a generic view, you're already using RequestContext).</li>
</ol>

Вопрос полезен? Да0/Нет0
file_4768.html(942.0б)
None

Ответы (12):

Ответvdboor:13.10.2014
Ответ полезен? Да0/Нет0

I've packed this snippet into django-debugtools. Hope that's useful for you too:

pip install django-debugtools
INSTALLED_APPS += ('debugtools',)
{% print_queries %}

For more examples, see https://github.com/edoburu/django-debugtools

Ответm:07.03.2013
Ответ полезен? Да0/Нет0

I still can't get it to work. I'm using Django 1.3.1. Can someone post their settings? I had to add a number of options to TEMPLATE_CONTEXT_PROCESSORS to get my site to render correctly. ("django.contrib.auth.context_processors.auth", "django.core.context_processors.debug", "django.core.context_processors.i18n", "django.core.context_processors.media", "django.core.context_processors.static", "django.contrib.messages.context_processors.messages")

I'm not clear where the template code goes. I copied it into my admin_base.html.

Ответlatisse-uk:03.10.2012
Ответ полезен? Да0/Нет0

Very useful

Latisse UK

Ответklaidi:22.07.2010
Ответ полезен? Да0/Нет0

nice !

Ответanalyzer:07.03.2010
Ответ полезен? Да0/Нет0

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

Ответramikassab:11.02.2009
Ответ полезен? Да0/Нет0

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

Ответaaronfay:24.09.2008
Ответ полезен? Да0/Нет0

Brilliant works like a charm :)

af

Ответdarek:13.07.2008
Ответ полезен? Да0/Нет0

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.

Ответphir:12.09.2007
Ответ полезен? Да0/Нет0

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.

Ответcookiebearo:28.05.2007
Ответ полезен? Да0/Нет0

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

Ответmsgre:14.03.2007
Ответ полезен? Да0/Нет0

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.

Ответjdobbins:11.03.2007
Ответ полезен? Да0/Нет0

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