Trying to build a state-machine that stores state in the model or in settings.py rather then in the database, I wrote this small generic pre_save hook that lets me leave all the data in the Model.
When developing a site, I sometimes want to be able to visually reference the HTML before it's given a real context, and a view. This extra bit of code will allow you to add a directory within your templates dir, and pull any html files and subdirectories from it. I added the if statement to automatically use an index.html file if there's no file and extension at the end. You can substitute anything you like for the "html" in "(?P<base>html)". That's just passed as an argument to prepend to the template as a base directory for the templates.
Better slugify function for national characters. Source of original script: http://trac.django-fr.org/browser/site/trunk/project/links/slughifi.py?rev=47
Dehydrates objects that can be dictionaries, lists or tuples containing django
model objects or django querysets. For each of those, it creates a
smaller/dehydrated version of it for saving in cache or pickling. The reverse
operation is also provided so dehydrated objects can also be re-hydrated.
*Example:*
>>> import pickle
>>> users = list(User.objects.all()[:20])
>>> print users
[<User: Indiana Jones>, <User: Bilbo Baggins>, ...]
>>> pickled_users = pickle.dumps(users)
>>> print len(pickled_users)
17546
>>> dehydrated_users = dehydrate(users)
>>> pickled_dehydrated_users = pickle.dumps(dehydrated_users)
>>> rehydrated_users = hydrate(pickle.loads(pickled_dehydrated_users))
>>> print rehydrated_users
[<User: Indiana Jones>, <User: Bilbo Baggins>, ...]
>>> print len(pickled_dehydrated_users)
1471
A version of http://djangosnippets.org/snippets/2388/ which assumes UTC-based dates.
This is when you store dates in UTC (as you should), and want to display them in your site's local timezone, and you notice that Django's timesince/time template tags still do not support timezones.
Memcached Property Attributes
-----------------------------
Setting the attribute will store the value in memcached; accessing the attribute will retrieve from memcached. Most useful as a way of simulating memcached "fields" on model instances. See the docstring for details.
This widget allows you to display preview images with adjustable width and length of the link:
[example](http://img526.imageshack.us/img526/6588/screenshotat20111026215.png)
AdvancedFileInput(preview=True, image_width=200)
For other files, you can adjust the length of the link without preview:
[example](http://img845.imageshack.us/img845/6588/screenshotat20111026215.png)
AdvancedFileInput(preview=False, url_length=30)
by default, parameters are:
preview = True
url_length = 30
image_width = 200
Django admin orders models by their primary key by default, which can be undesirable on very large tables.
This shows how to disable any ordering on a model.
Note that this behavior is fixed in 1.4 trunk.
A small script that takes a manage.py dumpdata generated json file, and removes fields of the specified models. I needed this because i kept my initial data on a json file and after I removed a field on one of my models, the script wouldn't work anymore.
If your application server is behind a proxy, `request.META["REMOTE_ADDR"]` will likely return the proxy server's IP, not the client's IP. The proxy server will usually provide the client's IP in the `HTTP_X_FORWARDED_FOR` header. This util function checks both headers. I use it behind Amazon's Elastic Load Balancer (ELB).
When using [django debug toolbar](https://github.com/django-debug-toolbar/django-debug-toolbar), I like to be able to turn debugging on and off without having to edit my settings file. This callback makes that possible. Add `?debug=on` to the URL to turn debugging on. It will remain on in the current session until you turn it off with `?debug=off`.
Make sure your session middleware comes before your debug toolbar middleware.
It was based in:
http://djangosnippets.org/snippets/1586/
Instead of doing this:
'attribute_name = forms.CharField(widget=forms.TextInput(attrs={'class':'special'}))`
You can do this in your template:
{{ form|cssclass:"attribute_name:special_class"|cssclass:"other_attribute:special_class" }}