from http://www.djangosnippets.org/snippets/792/
from utils.extjs import ExtJSONEncoder
from django.utils.safestring import mark_safe
class TestForm(forms.ModelForm):
class Meta:
model = TestModel
def as_ext(self):
return mark_safe(simplejson.dumps(self,cls=ExtJSONEncoder))
This code provides a primary key field that is globally unique. It uses the pre_save method to auto-populate the field with a Universal Unique Id as the record is saved the first time.
This uses the Really Simple Color Picker in jQuery:
http://www.web2media.net/laktek/2008/10/27/really-simple-color-picker-in-jquery/
Get source from there or GitHub:
http://github.com/laktek/really-simple-color-picker/tree/master
Easy way to generate image thumbnails for your models. Works with any Storage Backend.
From: [http://code.google.com/p/django-thumbs/](http://code.google.com/p/django-thumbs/)
**Usage example:**
==============
photo = ImageWithThumbsField(upload_to='images', sizes=((125,125),(300,200),)
To retrieve image URL, exactly the same way as with ImageField:
my_object.photo.url
To retrieve thumbnails URL's just add the size to it:
my_object.photo.url_125x125
my_object.photo.url_300x200
Note: The 'sizes' attribute is not required. If you don't provide it,
ImageWithThumbsField will act as a normal ImageField
**How it works:**
=============
For each size in the 'sizes' atribute of the field it generates a
thumbnail with that size and stores it following this format:
available_filename.[width]x[height].extension
Where 'available_filename' is the available filename returned by the storage
backend for saving the original file.
Following the usage example above: For storing a file called "photo.jpg" it saves:
photo.jpg (original file)
photo.125x125.jpg (first thumbnail)
photo.300x200.jpg (second thumbnail)
With the default storage backend if photo.jpg already exists it will use these filenames:
photo_.jpg
photo_.125x125.jpg
photo_.300x200.jpg
**Note:** It assumes that if filename "any_filename.jpg" is available
filenames with this format "any_filename.[widht]x[height].jpg" will be available, too.
Truncates a string after a given length, keeping the last word complete.
This filter is more precise than the default `truncatewords` filter.
Words length vary too much, 10 words may result in 40 or 70 characters, so cutting by character count makes more sense.
There is a [blog post](http://ricobl.wordpress.com/2008/12/23/templates-django-filtro-truncatewords-melhorado/) about this snippet (in Portuguese).
Some times I want to change the `owner` of an object to another user - problem is the object often has a lot of other objects pointing to them - I also want to update those fields.
This is a generic snippet for doing just that!
For instance:
change_owner(obj, new_owner_id):
return update_related_field(obj, new_owner_id, field="user")
Something I end up doing all the time, making a boolean variable show up as a nice image. With this code you can do the following:
`{% boolean_img user.is_active %}`
And get the following output:
`<img src="/media/icons/accept.png" alt="True" />`
All you need to do is use the custom templatetag code, load it in your template and use the `boolean_img` tag.
**Adjust templates, html and images where needed**
A simple way to log in and log out users. It's most likely nowhere near as good as the built-in login forms. This was more of a learning experience for me. Nevertheless, the script works.
Usage:
def some_view(request):
check_login(request)
Some explanation can be found here: http://bit.ly/ete0
My modified version of the [MultiQuerySet by mattdw](http://www.djangosnippets.org/snippets/1103/) (see the link for further information).
My purpose for this was to enable me to combine multiple different types of querysets together, which could then be iterated on as one object (i.e. like a tumblelog).
This middleware will look for the cookies set when a Facebook Connect user authenticates to your site, read those cookies, determine if the logged in user is your Facebook friend and then log that user into your Django-powered site.
If you don't need the bit about friend verification, it should be trivial to strip out.
There are a couple of other things that are needed to get FB Connect working with your site, and you can find a more detailed entry [here (http://nyquistrate.com/django/facebook-connect/)](http://nyquistrate.com/django/facebook-connect/).
Personally I hate using markdown for text input just so it can be converted into HTML. Markdown languages almost always don't support some thing I want to do; thus, why not just use HTML in the first place. Well because you don't want anybody posting any kind of HTML on your site.
Solution, instead of making your users learn markdown, let them enter HTML and filter out bad tags. This is a filter I use to filter HTML for only certain allowed tags. The allowed tags can be configured with the **allowedhtml** list.
To make your text input even more user friendly use a Javascript HTML editor like [FCK Editor](http://www.fckeditor.net/) so your users will have a nice GUI editor.