djangosnippets.org: Latest snippets posted by SmileyChrishttp://djangosnippets.org/users/SmileyChris/2010-07-15T22:06:07.991561-05:00SmileyChrisTestCase base class to easily temporarily change module values
2010-07-15T22:06:07.991561-05:00SmileyChrishttp://djangosnippets.org/snippets/2109/<ol>
<li><p>Base your test case off <code>ModuleTestCase</code> and set a class attribute containing a dictionary of modules which you want to be able to revert the values of.
</p>
</li>
<li><p>Use <code>self.modulename.attribute = something</code> in your <code>setUp</code> method or test cases to change the module's attribute values.
</p>
</li>
<li><p>The values will be automatically restored …</p></li></ol>
Freely redistributableNice form errors
2009-10-18T17:19:37.078611-05:00SmileyChrishttp://djangosnippets.org/snippets/1764/<p>Nicely output all form errors in one block, using field labels rather than the field attribute names.</p>
Freely redistributableTemplate tag to handle navigation item selection
2009-09-15T20:43:38.935270-05:00SmileyChrishttp://djangosnippets.org/snippets/1729/<p>Handles navigation item selection.</p>
<p>See the <code>nav</code> docstring for details.</p>
Freely redistributableFile storage with a better rename method
2009-07-30T03:28:19.086246-05:00SmileyChrishttp://djangosnippets.org/snippets/1658/<p>A file storage which uses a more sane rename method for existing files.
</p>
<p>Add <code>DEFAULT_FILE_STORAGE = 'site.storage.BetterNameFileSystemStorage'</code> (obviously changing <code>site.storage</code> to the module which you put this inside)
</p>
Freely redistributableSimple, stand-alone reCaptcha form module
2009-07-20T23:54:05.132004-05:00SmileyChrishttp://djangosnippets.org/snippets/1644/<p>An easy-to-use Django forms integration of the reCaptcha service.</p>
Freely redistributableShow users' full names for foreign keys in admin
2009-07-19T19:22:31.477045-05:00SmileyChrishttp://djangosnippets.org/snippets/1642/<p>This is a ModelAdmin base class you can use to make foreign key references to User a bit nicer in admin. In addition to showing a user's username, it also shows their full name too (if they have one and it differs from the username).</p>
<p><strong>2009-08-14</strong>: updated to handle …</p>
Freely redistributableLog in a user without requiring credentials
2009-06-02T17:10:27.574816-05:00SmileyChrishttp://djangosnippets.org/snippets/1547/<p>Rather than requiring a dummy backend (which I have seen some people do), use this method to log in a user without requiring their credentials.
</p>
Freely redistributableDigg-like pagination
2009-05-25T23:05:08.932712-05:00SmileyChrishttp://djangosnippets.org/snippets/1530/<p>My take on digg-like pagination.</p>
<p>Save the code as 'templatetags/pagination_nav.py' in one of your apps.</p>
<p>It relies on a 'pagination_nav.html' template. Here is a base template:</p>
<pre><code>{% if pages %}
<div class="bottom-pagination-nav">
{% if previous_url %}<a href="{{ previous_url }}">{% else %}<span>{% endif %}&laquo; Previous{% if previous_url %}</a>{% else %}</span>{% endif …</code></pre>
Freely redistributable"an" filter
2009-05-21T15:46:25.538860-05:00SmileyChrishttp://djangosnippets.org/snippets/1519/<p>A template filter which returns "a" or "an" depending on the phonetic value of given text.</p>
Freely redistributableDRYer instantiation of Forms
2009-04-07T15:48:54.175715-05:00SmileyChrishttp://djangosnippets.org/snippets/1420/<p>Using this small helper, you can instanciate your forms in an even DRYer way:
</p>
<pre><code>form = MyForm(**form_kwargs(request))
if form.is_valid():
#...
</code></pre>
Freely redistributableSmart {% if %} template tag
2009-03-02T20:46:53.783545-06:00SmileyChrishttp://djangosnippets.org/snippets/1350/<p>Save this as <code>smart_if.py</code> in the <code>templatetags</code> folder of one of your apps. Then a simple <code>{% load smart_if %}</code> replaces the boring built-in Django <code>{% if %}</code> template with the new smart one.</p>
<p><em>7 May 2009</em>: Was asked about whether it handles combination of and/or. It does, added …</p>
Freely redistributableGzip decorator
2008-07-24T18:20:26.315886-05:00SmileyChrishttp://djangosnippets.org/snippets/910/<p>Rather than using the full GZipMiddleware, you may want to just compress some views. This decorator lets you do that.</p>
<pre><code>@gzip_compress
def your_view(request, ...):
....
</code></pre>
Freely redistributableUnique Slugify
2008-04-08T20:03:18.595684-05:00SmileyChrishttp://djangosnippets.org/snippets/690/<p>Automatically create a unique slug for a model. </p>
<p>Note that you <em>don't</em> need to do <code>obj.slug = ...</code> since this method updates the instance's slug field directly. All you usually need is: <code>unique_slugify(obj, obj.title)</code></p>
<p>A frequent usage pattern is to override the <code>save</code> method of a model and call <code>unique_slugify …</code></p>
Freely redistributablepartition template filters
2007-09-02T22:31:12.526295-05:00SmileyChrishttp://djangosnippets.org/snippets/401/<p>A couple of useful template filters for splitting a list (or QuerySet) up into rows or columns.</p>
Freely redistributableEnhanced "avoid widows" template filters
2007-07-25T20:25:44.137065-05:00SmileyChrishttp://djangosnippets.org/snippets/340/<p>Building on <a href="http://www.djangosnippets.org/snippets/17/">jcroft's snippet</a>, here's a slightly more advanced version which has two filters, one for basic text and the other for html snippets.</p>
<p>Usage is like so:</p>
<pre><code><h2>{{ blog_entry.headline|escape|widont }}</h2>
{{ blog_entry.html|widont_html }}
</code></pre>
<p>On top of Jeff's reasons for using these filters, they are important because they help …</p>
Freely redistributable