If you want to resize transparent PNGs with the `{% thumbnail %}` templatetag, they'll sometimes get an ugly black background that looks even more ugly on a white background. This processor puts the image on a white background. You can simply change the background color by replacing `white` with any other color.
To use this filter simple put the following two lines of code in your settings file:
from sorl.thumbnail.defaults import PROCESSORS as THUMBNAIL_PROCESSORS
THUMBNAIL_PROCESSORS = ('path.to.white_background',) + THUMBNAIL_PROCESSORS
Here's some fairly normalized models for representing postal addresses. Making postal_code a separate model would probably be the only way to get it to a level that everyone would agree is 2NF, but we handle a lot of international addresses so that isn't really feasible.
Country and StateProvince are intended to be populated with data from ISO 3166. I'd include the SQL I used with that data, but it's very long and there's no attach feature. Also, I'd probably be violating ISO's copyright.
This code improves on Django's render_to_response shortcut function in the following ways:
1. If the caller does not provide a MIME type, and the caller is passing a RequestContext, it interrogates the request to determine if the HTTP client supports application/xhtml+xml encoding. If so, it sets the request type to application/xhtml+xml to override the HttpRequest class's default mime type of text/html.
2. It caches parsed templates in its own cache to improve performance.
If you aren't using XHTML in your templates, you may choose to comment out the code that tests for and sets the application/xhtml+xml MIME type.
I place this code in a file named "util.py" in my application. In my views, I write "from app.util import render_to_response" where I used to write "from django.shortcuts import render_to_response".
Note that the caching functionality provided by this code means that you will need to recycle your Django instance when you make template changes. Instructions for doing this depend on how you have deployed your Django application.
Template tag for displaying a list of arbitrary models. Useful for life-stream kind of pages that display blog entries, links, photos etc ordered by date. [Example](http://bjornkri.com)
**Usage:** something like:
{% for object in object_list %}
{% display_excerpt object %}
{% endfor %}
Will look for *app/model_excerpt.html* by default, and fall back on a generic *display_excerpt.html*, or returns the object's string representation as a last fallback.
*display_excerpt.html* might look something like:
<a href="{{ object.get_absolute_url }}">{{ object }}</a>
Any model you throw at it should have a *get_absolute_url* and a string representation of some sort, so this gives you the bare minimum of a title and a link to a detail page.
*display_excerpt* takes an optional argument to set the template suffix. This might be handy for generating different formatting for feeds, for instance:
{% for object in object_list %}
{% display_excerpt object "feed" %}
{% endfor %}
This will look for app/model_feed.html to render the object.
Got lots of help from mattmcc on #django for this one, thanks!
Sometimes I need to truncate a string after a number of characters, usually to avoid breaking the page layout. When the string we have to truncate is a filename I don't want to hide its extension so a user can easily recognize the file.
My solution is add the ellipsis at the middle of the string converting
`ALongLongLongTitleDocumentThatExemplifiesThisSnippet.txt`
into
`ALongLongLong...hisSnippet.txt`
As Simon Willison mentions in his [Debugging Django](http://simonwillison.net/2008/May/22/debugging/) presentation, using the Test Client in the interpreter can be a great way to take a peek at the raw results from a view. In some cases you may need to add additional headers to the request (for instance a piece of middleware may rely on them).
Though it is not mentioned in the reference documentation, a quick peek at the code confirmed my hopes that it would be possible to add data to the request. The Client *get* and *post* methods both accept an **extra** kwargs parameter that allow you to populate the request with additional data.
This is a _very basic_, _easily foolable_, restriction method implemented in a Django middleware.
However, for low security sites that need a cursory barrier to entry (without the ability to assign/administer user accounts), this does very well.
All of the features are fairly well-documented in the code.
Simple DecimalField class extension that automatically adds formatting and validation for comma-separated "decimals". Works wonderfully for price fields.
Could be extended to strip dollar signs or to be locale-agnostic.
when you deploy djangos apps, some servers have problems resolving the absolute path of some files (e.g: sqlite3 + lighttpd + apache), using the snippet above solves this issue :)
As users would login to their accounts to update their CC info, the expiration date always threw them off. The default format for displaying a datetime.date object is
>YYYY-MM-DD
Obviously the expiration date on your credit card uses the MM/YY format. I finally got around to creating a custom field/widget to handle this particular piece of data.
Use like so...
class CustomerForm(forms.ModelForm):
cc_exp = DateFieldCCEXP()
class Meta:
model = Customer
An abstract model base class that gives your models a random base-32 string ID. This can be useful in many ways. Requires a Django version recent enough to support model inheritance.
A Django view to create an image gradient on the fly as a PNG file. The direction, size and colors of the gradient a specified in the filename and extracted by Django in the urls.py.
Example usage from CSS:
`background: url(/gradient-down-255,255,255-to-0,0,0-70-of-120.png) repeat-x;` creates a 70-pixel vertical gradient as background from white to gray. No static images needed. To modify, nothing but the CSS needs to be edited.
You're looking at the most-bookmarked snippets on the site; if you'd like to help useful snippets show up here, sign up for an account and you'll get your own bookmarks list.