Inspired by http://www.djangosnippets.org/snippets/159/
This context processor provides a new variable {{ sqldebug }}, which can
be used as follows:
{% if sqldebug %}...{% endif %}
{% if sqldebug.enabled %}...{% endif %}
This checks settings.SQL_DEBUG and settings.DEBUG. Both need to be True,
otherwise the above will evaluate to False and sql debugging is considered
to be disabled.
{{ sqldebug }}
This prints basic information like total number of queries and total time.
{{ sqldebug.time }}, {{ sqldebug.queries.count }}
Both pieces of data can be accessed manually as well.
{{ sqldebug.queries }}
Lists all queries as LI elements.
{% for q in sqldebug.queries %}
<li>{{ q.time }}: {{ q }}</li>
{% endfor %}
Queries can be iterated as well. The query is automatically escaped and contains
<wbr> tags to improve display of long queries. You can use {{ q.sql }} to access
the unmodified, raw query string.
Here's a more complex example. It the snippet from:
http://www.djangosnippets.org/snippets/93/
adjusted for this context processor.
{% if sqldebug %}
<div id="debug">
<p>
{{ sqldebug.queries.count }} Quer{{ sqldebug.queries|pluralize:"y,ies" }}, {{ sqldebug.time }} seconds
{% 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 sqldebug.queries %}<tr class="{% cycle odd,even %}">
<td>{{ forloop.counter }}</td>
<td>{{ query }}</td>
<td>{{ query.time }}</td>
</tr>{% endfor %}
</tbody>
</table>
</div>
{% endif %}
- sql
- debug
- queries
- db
- database
- contextprocessor
A variation on a theme, inspired by [snippet 39][39] and [snippet 119][119]. The
intent is to provide a more generic and simple mechanism for combining
[Markdown][markdown] with [Pygments][pygments]. Common scenarios could include blogging or commenting. Snippet 119 seemed too specific and perhaps not as
efficient, needing to process the HTML twice to accomplish it's ends. The one snag in the implementation is the need to use a tag other than `code` as a wrapper. See the comments for details.
You will need the [BeautifulSoup][soup] module installed.
Sample usage:
from django.db import models
class Blog(models.Model):
'''Bare bones blogging model'''
title = models.CharField(maxlength=255)
slug = models.SlugField(maxlength=255, prepopulate_from=('title',))
pub_date = models.DateTimeField()
# the cooked view, cached for quick retrieval
blog = models.TextField()
# the raw markdown-encoded text, saved for subsequent edits
markdown = models.TextField()
def save(self):
from datetime import datetime
if not self.id and not self.pub_date:
self.pub_date = datetime.now()
self.blog = pygmented_markdown(self.markdown)
super(Blog, self).save()
[39]: http://www.djangosnippets.org/snippets/39/
[119]: http://www.djangosnippets.org/snippets/119/
[soup]: http://www.crummy.com/software/BeautifulSoup/
[markdown]: http://www.freewisdom.org/projects/python-markdown/Installation
[pygments]: http://pygments.org/
- pygments
- beautifulsoup
- markdown