djangosnippets.org: Latest snippets posted by peterbehttps://djangosnippets.org/users/peterbe/2010-09-08T16:17:34.677776-05:00peterbeOne step up from __icontains
2010-09-08T16:17:34.677776-05:00peterbehttps://djangosnippets.org/snippets/2188/<p>The <a href="http://docs.djangoproject.com/en/dev/ref/models/querysets/#iregex">REGEX and IREGEX</a> operators were added in Django 1.0 and I'm sure you can think of fancier ways of doing word delimiting and things like that but this was all I needed to make a user-friendly autocomplete search function.
</p>
Freely redistributableReset cache between tests
2009-11-11T11:05:57.696074-06:00peterbehttps://djangosnippets.org/snippets/1789/<p>I don't understand why the cache is accumulated between the tests. I thought one of the axioms of unit testing is that the tests should not affect each other. </p>
<p>Couldn't find anything that explains why it's done this way but it seems a bit strange. Anybody know if there's a …</p>
Freely redistributableUse git log to give your app a revision
2009-10-30T07:40:07.062421-05:00peterbehttps://djangosnippets.org/snippets/1777/<p>Put that stuff in your settings.py
Then you can use the git log last date to make that your "revision number" assuming you don't use regular version release numbers. With it you can do something like:</p>
<pre><code><div id="footer">
&copy; My Company
&mdash; The Super App (revision {{ GIT_REVISION_DATE }})
</div> …</code></pre>
Freely redistributablecache_page that does nothing
2009-08-20T11:10:27.929364-05:00peterbehttps://djangosnippets.org/snippets/1693/<p>When debugging/developing you want to be able to refresh your views every time you make a little change. But when in production mode you might want to cache these views because they contain long and resource hungry calculations or something.</p>
<p>By putting this above "hack" in after importing <code>cache_page</code> you …</p>
Freely redistributablePretty print SQL of query sets
2009-05-05T04:24:34.346358-05:00peterbehttps://djangosnippets.org/snippets/1484/<p>Install <a href="http://code.google.com/p/python-sqlparse/">sqlparse</a> with <code>easy_install sqlparse</code> and then you can easily debug the SQL like this:</p>
<pre><code>def view(request):
data = MyModel.objects.filter(something__very=complex)
print_sql(data)
...
</code></pre>
<p>Inspired by <a href="http://simonwillison.net/2009/Apr/28/">Simon Willison</a></p>
Freely redistributable"Zoom in" on rendered HTML that the test client returns
2009-04-21T15:20:13.610077-05:00peterbehttps://djangosnippets.org/snippets/1455/<p>If you have this as your base class for all unit tests you can do the following:</p>
<pre><code>class TestViews(BaseTestCase):
def test_generated_stats(self):
"test that certain stuff in the response"
...create some content for testing or use fixtures...
response = self.client.get('/some/page/')
# At this point response.content is a huge string filled with …</code></pre>
Freely redistributablewhitespaceoptimize block tag
2008-09-14T05:27:21.115324-05:00peterbehttps://djangosnippets.org/snippets/1055/<p>This is a custom block tag and is used like this:</p>
<pre><code>{% load whitespaceoptimize %}
{% whitespaceoptimize "css" %}
/* CSS comment */
body {
color: #CCCCCC;
}
{% endwhitespaceoptimize %}
</code></pre>
<p>And when rendered you get this output:</p>
<pre><code>body{color:#CCC}
</code></pre>
<p>To install it, download the snippet and call it <code>myapp/templatetags/whitespaceoptimize.py</code>. …</p>
Freely redistributable