Login

Snippets by SmileyChris

Snippet List

TestCase base class to easily temporarily change module values

1. Base your test case off `ModuleTestCase` and set a class attribute containing a dictionary of modules which you want to be able to revert the values of. 2. Use `self.modulename.attribute = something` in your `setUp` method or test cases to change the module's attribute values. 3. The values will be automatically restored when each test case finishes. For the common case of reverting the settings module, just use the `SettingsTestCase` as your base class.

  • tests
Read More

Nice form errors

Nicely output all form errors in one block, using field labels rather than the field attribute names.

  • forms
  • template-filter
  • errors
Read More

File storage with a better rename method

A file storage which uses a more sane rename method for existing files. Add `DEFAULT_FILE_STORAGE = 'site.storage.BetterNameFileSystemStorage'` (obviously changing `site.storage` to the module which you put this inside)

  • file-storage
Read More

Show users' full names for foreign keys in admin

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). **2009-08-14**: updated to handle many to many fields and easily configure whether to always show the username (if it differs from full name)

  • user
  • modeladmin
  • get_full_name
Read More

Digg-like pagination

My take on digg-like pagination. Save the code as 'templatetags/pagination_nav.py' in one of your apps. It relies on a 'pagination_nav.html' template. Here is a base template: {% 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 %} {% for group in pages %} {% for page in group %} {% if page.current %}<span>{{ page.number }}</span>{% else %}<a href="{{ page.url }}">{{ page.number }}</a>{% endif %} {% endfor %} {% if not forloop.last %}<span>...</span>{% endif %} {% endfor %} {% if next_url %}<a href="{{ next_url }}">{% else %}<span>{% endif %}Next &raquo;{% if next_url %}</a>{% else %}</span>{% endif %} </div> {% endif %}

  • pagination
  • template-tag
  • paginator
  • digg
Read More

"an" filter

A template filter which returns "a" or "an" depending on the phonetic value of given text.

  • template-filter
  • grammar
Read More

Smart {% if %} template tag

Save this as `smart_if.py` in the `templatetags` folder of one of your apps. Then a simple `{% load smart_if %}` replaces the boring built-in Django `{% if %}` template with the new smart one. *7 May 2009*: Was asked about whether it handles combination of and/or. It does, added a test to show it. I actually like how Django doesn't let you do this, but I'm not going to confuscate my code for a restriction like this. *15 June 2009*: Fixed up a bug with boolean precedence (`x or x == 0` was being parsed as `(x or x) == 0` instead of `x or (x == 0)`). Add some extra test cases, including some for invalid cases.

  • if
  • smart-if
  • if-in
  • greater-than
  • less-than
Read More

Gzip decorator

Rather than using the full GZipMiddleware, you may want to just compress some views. This decorator lets you do that. @gzip_compress def your_view(request, ...): ....

  • middleware
  • decorator
  • gzip
Read More

Unique Slugify

Automatically create a unique slug for a model. Note that you *don't* need to do `obj.slug = ...` since this method updates the instance's slug field directly. All you usually need is: `unique_slugify(obj, obj.title)` A frequent usage pattern is to override the `save` method of a model and call `unique_slugify` before the `super(...).save()` call.

  • slug
Read More

Enhanced "avoid widows" template filters

Building on [jcroft's snippet](http://www.djangosnippets.org/snippets/17/), here's a slightly more advanced version which has two filters, one for basic text and the other for html snippets. Usage is like so: <h2>{{ blog_entry.headline|escape|widont }}</h2> {{ blog_entry.html|widont_html }} On top of Jeff's reasons for using these filters, they are important because they help keep one of [God's commandments](http://www.ebible.com/bible/NIV/Exodus+22%3A22). ;)

  • filter
  • widows
  • typography
  • widont
Read More

SmileyChris has posted 20 snippets.