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" }}
Actually the best way to handle this is to use built-in http://docs.haystacksearch.org/dev/searchqueryset_api.html#load-all
I just missed it while checking documentation and wrote this crappy snippet!
A FilterSpec that can be used to filter by taggit tags in the admin.
To use, simply import this module (for example in `models.py`), and add the name of your TaggableManager field in the
list_filter attribute of your ModelAdmin class.
**Your model:**
class TicketItem(models.Model):
hours = models.DecimalField(decimal_places=2, max_digits=6)
day = models.DateField()
order = models.ForeignKey(Order)
tickets = models.ManyToManyField(Ticket)
Now you want to auto save m2m fields in your forms.TicketItemCreateForm:
1. inherit from m2mForm-Class
2. define m2m_field(s)
**Example:**
class TicketItemCreateForm(m2mForm):
m2m_field = 'tickets'
class Meta:
model = models.TicketItem
Renders enclosing contents as it is. Useful to avoid tags conflict with certain javascript libraries (eg. jquery-tmpl.js, mustache.js)
Example usage:
`<script class="content-tmpl" type="text/x-jquery-tmpl">`
`{% alien %}`
`{{ tmpl(results) "#results .item-tmpl" }}`
`{% endalien %}`
`</script>`